public HttpResponseMessage Post([FromBody] DiaryModel model) { try { string username = _identityService.CurrentUser; if (TheRepository.GetDiaries(username).Count(d => d.CurrentDate == model.CurrentDate.Date) > 0) { return(Request.CreateErrorResponse(HttpStatusCode.Conflict, "A diary already exists for that date")); } Diary diary = TheModelFactory.Parse(model); diary.UserName = username; if (TheRepository.Insert(diary) && TheRepository.SaveAll()) { return(Request.CreateResponse(HttpStatusCode.Created, TheModelFactory.Create(diary))); } } catch { // TODO Add Logging } return(Request.CreateResponse(HttpStatusCode.BadRequest)); }
public HttpResponseMessage Post([FromBody] DiaryModel model) { try { Diary entity = TheModelFactory.Parse(model); if (entity == null) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not read diary body")); } //Check if diary exists for the current user. if (TheRepository.GetDiaries(_identityService.CurrentUser).Count(d => d.CurrentDate == model.CurrentDate.Date) > 0) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Duplicate diaries not allowed")); } entity.UserName = _identityService.CurrentUser; if (TheRepository.Insert(entity) && TheRepository.SaveAll()) { return(Request.CreateResponse(HttpStatusCode.Created, TheModelFactory.Create(entity))); } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could Not Save to the Database")); } } catch (Exception e) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.ToString())); } }
public HttpResponseMessage Get(DateTime date) { string username = _identityService.CurrentUser; Diary diary = TheRepository.GetDiary(username, date); if (diary == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "No diary found")); } DiaryModel diaryModel = TheModelFactory.Create(diary); return(Request.CreateResponse(HttpStatusCode.OK, diaryModel)); }