public HttpResponseMessage InsertOrUpdateComment(ProjectCommentModel projectComment, int projectId, int commentId = 0) { if (!ModelState.IsValid || projectId <= 0 || commentId < 0) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } var identity = User.Identity as ClaimsIdentity; using (var s = new ProjectCommentRepository()) { var httpStatusCode = HttpStatusCode.Created; //insert comment if (commentId.Equals(0)) { ProjectCommentRepository.StatusCodes hasInserted = s.InsertComment(projectComment, identity, projectId); switch (hasInserted) { //project not found to insert the comment case ProjectCommentRepository.StatusCodes.NOT_FOUND: httpStatusCode = HttpStatusCode.NotFound; break; //comment inserted ok case ProjectCommentRepository.StatusCodes.OK: httpStatusCode = HttpStatusCode.Created; break; } } //update existing comment else { ProjectCommentRepository.StatusCodes hasUpdated = s.UpdateComment(projectComment, identity, commentId); switch (hasUpdated) { //comment not found case ProjectCommentRepository.StatusCodes.NOT_FOUND: httpStatusCode = HttpStatusCode.NotFound; break; //not authorized to update this comment case ProjectCommentRepository.StatusCodes.NOT_AUTHORIZED: httpStatusCode = HttpStatusCode.MethodNotAllowed; break; //comment updated ok case ProjectCommentRepository.StatusCodes.OK: httpStatusCode = HttpStatusCode.OK; break; } } return(Request.CreateResponse(httpStatusCode)); } }
public static ProjectComment MapProjectCommentModel(ProjectCommentModel model) { var coreModel = new ProjectComment(); coreModel.Id = model.Id; coreModel.ProjectCommentAreaId = model.ProjectCommentAreaId; coreModel.ProjectCommentTypeId = model.ProjectCommentTypeId; coreModel.ProjectId = model.ProjectId; coreModel.Date = model.Date.GetMyLocalTime(); coreModel.Text = model.Text; return(coreModel); }
public ProjectCommentViewModel CreateProjectComment(ProjectCommentModel comment) { //map the incoming model to to a coreModel for insert var coreModel = ProjectCommentMapper.MapProjectCommentModel(comment); //insert the core model and return a core view var projectCommentView = commentRepository.CreateComment(coreModel); //map the core view to a model view var projectCommentViewModel = ProjectCommentMapper.MapProjectComment(projectCommentView); //return the model view return(projectCommentViewModel); }
// OK public StatusCodes InsertComment(ProjectCommentModel source, ClaimsIdentity identity, int projectId) { try { //get the project var _project = uow.ProjectRepository.FindById(projectId); if (_project == null) { return(StatusCodes.NOT_FOUND); } else { long requestorUserId; try { requestorUserId = uow.UserRepository .SearchFor(e => e.Username == identity.Name) .Select(e => e.Id) .SingleOrDefault(); } catch (InvalidOperationException ex) { throw new InvalidOperationException("User lookup for requestor Id for project comment creation failed", ex); } var _projectComment = new ProjectComment() { ProjectId = projectId, UserId = requestorUserId, AttachmentSetId = source.AttachmentSetId, WhenDateTime = DateTime.Now, Description = source.Description }; uow.ProjectCommentreRepository.Insert(_projectComment, true); } return(StatusCodes.OK); } catch (Exception) { throw; } }
// OK public StatusCodes UpdateComment(ProjectCommentModel source, ClaimsIdentity identity, int commentId) { try { var _projectComment = uow.ProjectCommentreRepository.FindById(commentId); if (_projectComment == null) { //comment not found return(StatusCodes.NOT_FOUND); } else { // comment found. does the user that wishes to update it really is the comment creator? check this here long requestorUserId; try { requestorUserId = uow.UserRepository .SearchFor(e => e.Username == identity.Name) .Select(e => e.Id) .SingleOrDefault(); } catch (InvalidOperationException ex) { throw new InvalidOperationException("User lookup for requestor Id for project comment edit failed", ex); } if (_projectComment.UserId != requestorUserId) { return(StatusCodes.NOT_AUTHORIZED); } _projectComment.WhenDateTime = DateTime.Now; _projectComment.Description = source.Description; uow.ProjectCommentreRepository.Update(_projectComment, true); } return(StatusCodes.OK); } catch (Exception) { throw; } }
public string CommentProject(int projectID, string comment) { var ctx = new OruBloggenDbContext(); var userID = User.Identity.GetUserId(); var userName = ctx.Users.FirstOrDefault(u => u.UserID == userID).UserFirstname + " " + ctx.Users.FirstOrDefault(u => u.UserID == userID).UserLastname; var commentObject = new ProjectCommentModel { Comment = comment, CommentDate = DateTime.Now, ProjectID = projectID, UserCommentID = userID, UserCommentName = userName }; ctx.ProjectComments.Add(commentObject); ctx.SaveChanges(); return(userName); }
public void EditComment(ProjectCommentModel comment) { commentRepository.EditComment(comment.Id, comment.Text); }