public HttpResponseMessage Patch(DateTime diaryid, int id, [FromBody] DiaryEntryModel model) { try { var entity = TheRepository.GetDiaryEntry(_identityService.CurrentUser, diaryid, id); if (entity == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } var parsedValue = TheModelFactory.Parse(model); if (parsedValue == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "not valid value")); } if (entity.Quantity != parsedValue.Quantity) { entity.Quantity = parsedValue.Quantity; if (TheRepository.SaveAll()) { LogActivity.LogSend("patch", Request); return(Request.CreateResponse(HttpStatusCode.OK)); } } return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "fail")); } catch (Exception e) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e)); } }
public HttpResponseMessage Delete(DateTime diaryid, int id) { try { if (TheRepository.GetDiaryEntries(_identityService.CurrentUser, diaryid).Any(e => e.Id == id) == false) { return(Request.CreateResponse(HttpStatusCode.NotFound, "not found")); } if (TheRepository.DeleteDiaryEntry(id) && TheRepository.SaveAll()) { LogActivity.LogSend("delete", Request); return(Request.CreateResponse(HttpStatusCode.OK)); } else { return(Request.CreateResponse(HttpStatusCode.BadRequest, "not delete")); } } catch (Exception exception) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, exception)); } }
public HttpResponseMessage Post(DateTime diaryid, [FromBody] DiaryEntryModel model) { try { var entity = TheModelFactory.Parse(model); if (entity == null) { Request.CreateErrorResponse(HttpStatusCode.BadRequest, "cannot found entry"); } var diary = TheRepository.GetDiary(_identityService.CurrentUser, diaryid); if (diary == null) { Request.CreateResponse(HttpStatusCode.NotFound); } //check duplicate if (diary.Entries.Any(e => e.Measure.Id == entity.Measure.Id)) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "duplicate")); } //save new entry diary.Entries.Add(entity); if (TheRepository.SaveAll()) { LogActivity.LogSend("post", Request); return(Request.CreateResponse(HttpStatusCode.OK, TheModelFactory.Create(entity))); } else { return(Request.CreateResponse(HttpStatusCode.BadRequest, "not added")); } } catch (Exception exception) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, exception)); } }
// GET api/values/5 public string Get(int id) { LogActivity.LogSend("post", Request); return(""); }