public async Task <IActionResult> CreateOrUpdate([FromBody] IdeaNewViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = await _ideaService.CreateOrUpdateAsync(model); if (!result.Succeeded) { return(BadRequest(result.Errors)); } return(Ok()); }
public async Task <ProcessResult> CreateOrUpdateAsync(IdeaNewViewModel model) { Func <Task> action = async() => { var ideaEntity = await GetOrCreateEntityAsync(context.Ideas, x => x.Id == model.Id); var idea = ideaEntity.result; if (!ideaEntity.isCreated && CurrentUser.Id != idea.CreatorId) { throw new InvalidOperationException("You're not owner of requested idea"); } idea.Title = model.Title; idea.Article = model.Article; await context.SaveChangesAsync(); // find or create-new tag and connect it to idea for (int i = 0; i < model.Tags.Count; i++) { var tagEntity = await GetOrCreateEntityAsync(context.Tags, x => x.Name == model.Tags[i]); var tag = tagEntity.result; if (tagEntity.isCreated) { tag.Name = model.Tags[i]; } await context.SaveChangesAsync(); var ideaTag = (await GetOrCreateEntityAsync(context.relIdeaTags)).result; ideaTag.IdeaId = idea.Id; ideaTag.TagId = tag.Id; await context.SaveChangesAsync(); } }; return(await Process.RunInTransactionAsync(action, context)); }