public async Task <IActionResult> PostAsync([FromBody] Tag newTag, string uuid) { if (!ModelState.IsValid) { return(BadRequest(ModelState.GetErrorMessages())); } //Try to fetch the metadata type, and return 404 if it doesnt exist ExperiencePost post = null; try { post = await _experiencePostService.GetByUuidAsync(Guid.Parse(uuid)); } catch (Exception) { throw new HttpException(HttpStatusCode.NotFound); } // Same but with tags Tag tag = null; try { tag = await _tagService.GetByNameAsync(newTag.Name); } catch (Exception) { throw new HttpException(HttpStatusCode.NotFound); } post.Tags.Add(new ExperiencePostTagMapping { Tag = tag, Post = post }); var res = _mapper.Map <ExperiencePost, ExperiencePostResource>(post); await _unitOfWork.CompleteAsync(); return(Ok(res)); }
public async Task <SaveExperiencePostResponse> SaveAsync(ExperiencePost post) { try { await _experiencePostRepository.AddAsync(post); return(new SaveExperiencePostResponse(post)); } catch (Exception ex) { // Do some logging stuff return(new SaveExperiencePostResponse($"An error occurred when saving the experience post: {ex.Message}")); } }
public async Task AddAsync(ExperiencePost experiencePost) { await _context.ExperiencePosts.AddAsync(experiencePost); }
/// <summary> /// Creates a success response. /// </summary> /// <param name="experiencePost">Saved experiencePost.</param> /// <returns>Response.</returns> public SaveExperiencePostResponse(ExperiencePost experiencePost) : this(true, string.Empty, experiencePost) { }
private SaveExperiencePostResponse(bool success, string message, ExperiencePost experiencePost) : base(success, message) { ExperiencePost = experiencePost; }