public async Task <IModelServiceResponse <Comment> > Create(ActionContext actionContext, Comment comment) { comment = comment.Clone(); string userID = GetUserID(actionContext.HttpContext.User); if (userID != "") { comment.UserID = userID; } ValidationStateDictionary validationState = TryValidateModel(actionContext, comment); if (!validationState.IsValid()) //If Comment does not have validation errors { return(new ModelServiceResponse <Comment>(comment, validationState, HttpStatusCode.BadRequest)); } try { //Save the comment to the database _context.Add(comment); await _context.SaveChangesAsync(); // return(new ModelServiceResponse <Comment>(comment, validationState, HttpStatusCode.Created)); } catch { // return(new ModelServiceResponse <Comment>(null, validationState, HttpStatusCode.InternalServerError)); } }
private async Task <IModelServiceResponse <Comment> > Update(ActionContext actionContext, Comment comment) { if (Authorization.IsAuthorizedEditor(comment, actionContext.HttpContext.User)) { return(new ModelServiceResponse <Comment>(comment, null, HttpStatusCode.Unauthorized)); } ValidationStateDictionary validationState = TryValidateModel(actionContext, comment); if (!validationState.IsValid()) { return(new ModelServiceResponse <Comment>(comment, validationState, HttpStatusCode.BadRequest)); } try { _context.Update(comment); await _context.SaveChangesAsync(); return(new ModelServiceResponse <Comment>(comment, validationState, HttpStatusCode.OK)); } catch { return(new ModelServiceResponse <Comment>(null, validationState, HttpStatusCode.BadRequest)); } }