Part 5 - Create CalendarEvent and EventsController Classes

In this task you will create an object model and a new Events Controller

1. For our Calendar App we’ll need a POCO class to represent a calendar event, so let’s add a new class to the project (right-click the Project -> Add -> Class) and name the class CalendarEvent.cs and add the below code

public class CalendarEvent

{

	public string id { get; set; }

	public string title { get; set; }

	public bool allDay { get; set; }

	public DateTime start { get; set; }

	public DateTime end { get; set; }

}

The class should look like:

2. Now we need to add a new controller class that maps the required properties of a calendar event so let’s add a new Controller to the project by right-clicking the Controllers folder and selecting -> Add -> Controller

3. Select API Controller-Empty and click Add

4. Name the controller ‘EventController’ and click Add

5. Let’s first add the following to the using section:

  1. Add the following line to the using section:

    using Microsoft.AspNetCore.Authorization;
    

The using section should look like:

6. Now let’s replace the EventsController.cs code with the following:

public class EventsController : Controller
{

	private static List<CalendarEvent> events;

	public EventsController()
	{

		if (events == null)
		{
			var calEvent = new CalendarEvent()
			{
				id = Guid.NewGuid().ToString(),
				title = "Test Event Title",
				allDay = false,
				start = DateTime.Now,
				end = DateTime.Now.AddHours(2)
			};
			events = new List<CalendarEvent>( new[] { calEvent } );
		}
	}

	// GET api/events
	[HttpGet]
	[Authorize(Policy = "InCalendarWriterGroup")]
	public IEnumerable<CalendarEvent> Get(DateTime? start, DateTime? end)
	{
		start = start ?? new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
		end = end ?? new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month));
		return events.FindAll(x => x.end >= start && x.start <= end);
	}

	// POST api/events

	[HttpPost]
	[Authorize(Policy = "InCalendarWriterGroup")]
	public void Post([FromForm]CalendarEvent calEvent)
	{
		calEvent.id = Guid.NewGuid().ToString();
		events.Add(calEvent);
	}

	// PUT api/events/5
	[HttpPut("{id}")]
	[Authorize(Policy = "InCalendarWriterGroup")]
	public void Put(string id, [FromForm]CalendarEvent calEvent)
	{
		var index = events.FindIndex(x => x.id == id);
		events[index] = calEvent;
	}
}

The class should look like:

Info: FullCalendar event is an open-source JavaScript calendar built on JQuery, which we’ll use in the client-side portion of the project

In the EventsController class we have implementations for a Get() method, a Post() method, and a Put() method that do the following:

  • Get() method that returns all calendar events in a date range

  • Post() method to create new calendar events

  • Put() method to edit existing calendar events

And we decorate the Get(), Put() and Post() methods with our custom [Authorize] attribute and specify the InCalendarWriterGroup policy we registered in Startup.ConfigureServices

Info: To keep things really simple, for this walk-thru you will just be storing events in a static collection in the controller. In a real application you would persist them somewhere durable and share between different Lambda invocations like Amazon DynamoDB a serverless NoSQL database. Lambda functions will be unloaded after about 5 minutes of non-use, so any events you add will disappear when that happens

Note: the test event created in the constructor is used to demonstrate the ability to see events when not authenticated