void ValidateRequest(ResolveViewModel model) { if (model.Sources.HasItems()) { foreach (var item in model.Sources) { if (item.NoteId > 0) { var note = _notesService.GetById(item.NoteId); if (note == null) { ModelState.AddModelError("sources", $"錯誤的參考. Note Id: ${item.NoteId}"); return; } } else { var term = _termsService.GetById(item.TermId); if (term == null) { ModelState.AddModelError("sources", $"錯誤的參考. Term Id: ${item.TermId}"); return; } } } } else { if (String.IsNullOrEmpty(model.Text) && model.Attachments.IsNullOrEmpty()) { ModelState.AddModelError("text", "必須填寫內容"); return; } } }
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 static Resolve MapEntity(this ResolveViewModel model, IMapper mapper, string currentUserId) { var entity = mapper.Map <ResolveViewModel, Resolve>(model); if (model.Id == 0) { entity.SetCreated(currentUserId); } entity.SetUpdated(currentUserId); if (!entity.Text.HasHtmlTag()) { entity.Text = entity.Text.ReplaceNewLine(); } entity.Highlight = model.Highlights.HasItems() ? JsonConvert.SerializeObject(model.Highlights) : ""; entity.Source = model.Sources.HasItems() ? JsonConvert.SerializeObject(model.Sources) : ""; return(entity); }
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))); }