public IActionResult Post([FromHeader(Name = "ApiKey")] string carerApiKey, [FromQuery(Name = "id")] string patientId, [FromBody] JObject calendarJson) { try { var jsonDict = JObject.FromObject(calendarJson).ToObject <Dictionary <string, object> >(); DateTime start; DateTime end; try { // Datetime object. start = (DateTime)jsonDict["Start"]; end = (DateTime)jsonDict["End"]; } catch { // String representation of datetime. start = DateTime.Parse((string)jsonDict["Start"]); bool endProvided = DateTime.TryParse((string)jsonDict["End"], out end); if (!endProvided) { end = start; } } bool validEntry = (start > DateTime.Now && start <= end); if (validEntry) { bool patientAssignedToThisCarer = DbService.PatientIsAssigned(_ctx, carerApiKey, patientId); if (patientAssignedToThisCarer) { bool success = DbService.AddCalendarEntry(_ctx, patientId, jsonDict).GetAwaiter().GetResult(); if (success) { return(Ok("Successfully added calendar entry.")); } return(BadRequest("Failed to add calendar entry.")); } return(Unauthorized("You are not assigned to this patient.")); } return(BadRequest("Invalid start date.")); } catch (Exception e) { return(BadRequest(e)); } }