public async Task <ActionResult> Update(int id, [FromBody] ResolveViewModel model) { var existingEntity = await _resolvesService.GetByIdAsync(id); if (existingEntity == null) { return(NotFound()); } ValidateRequest(model); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var resolve = model.MapEntity(_mapper, CurrentUserId); resolve.Reviewed = true; await _resolvesService.UpdateAsync(existingEntity, resolve); if (model.Attachments.HasItems()) { var attachments = model.Attachments.Select(item => item.MapEntity(_mapper, CurrentUserId)).ToList(); foreach (var attachment in attachments) { attachment.PostType = PostType.Resolve; attachment.PostId = resolve.Id; } await _attachmentsService.SyncAttachmentsAsync(resolve, attachments); resolve.Attachments = attachments; } else { await _attachmentsService.SyncAttachmentsAsync(resolve, null); } var reviewRecord = new ReviewRecord { Reviewed = true, Type = ReviewableType.Resolve, PostId = resolve.Id }; reviewRecord.SetCreated(CurrentUserId); await _reviewRecordsService.CreateAsync(reviewRecord); return(Ok()); }
public async Task <ActionResult> Store([FromBody] ResolveViewModel model) { ValidateRequest(model); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var resolve = model.MapEntity(_mapper, CurrentUserId); resolve.Reviewed = true; resolve = await _resolvesService.CreateAsync(resolve); if (model.Attachments.HasItems()) { var attachments = model.Attachments.Select(item => item.MapEntity(_mapper, CurrentUserId)).ToList(); foreach (var attachment in attachments) { attachment.PostType = PostType.Resolve; attachment.PostId = resolve.Id; } _attachmentsService.CreateMany(attachments); resolve.Attachments = attachments; } var reviewRecord = new ReviewRecord { Reviewed = true, Type = ReviewableType.Resolve, PostId = resolve.Id }; reviewRecord.SetCreated(CurrentUserId); await _reviewRecordsService.CreateAsync(reviewRecord); return(Ok(resolve.MapViewModel(_mapper))); }