public async Task <IActionResult> Add( [FromBody] LoverAnniversaryAddResource addResource) { if (!ModelState.IsValid) { return(UnprocessableEntity(ModelState)); } LoverCloudUser user = await _userRepository.FindByIdAsync(this.GetUserId()); if (user.Lover == null) { return(this.UserNoLoverResult(user)); } LoverAnniversary anniversary = _mapper.Map <LoverAnniversary>(addResource); anniversary.Lover = user.Lover; _anniversaryRepository.Add(anniversary); if (!await _unitOfWork.SaveChangesAsync()) { throw new Exception("Failed to add anniversary"); } ExpandoObject anniversaryResource = _mapper.Map <LoverAnniversaryResource>(anniversary) .ToDynamicObject(); (anniversaryResource as IDictionary <string, object>).Add( "links", CreateLinksForAnniversary(anniversary.Id)); return(CreatedAtRoute("AddLoverAnniversary", anniversaryResource)); }
public async Task <IActionResult> PartiallyUpdate( [FromRoute] string id, [FromBody] JsonPatchDocument <LoverAnniversaryUpdateResource> patchDoc) { LoverAnniversary anniversary = await _anniversaryRepository.FindByIdAsync(id); if (anniversary == null) { return(NotFound()); } var authorizationResult = await _authorizationService.AuthorizeAsync( User, anniversary, Operations.Update); if (!authorizationResult.Succeeded) { return(Forbid()); } LoverAnniversaryUpdateResource anniversaryResource = _mapper.Map <LoverAnniversaryUpdateResource>(anniversary); patchDoc.ApplyTo(anniversaryResource); _mapper.Map(anniversaryResource, anniversary); return(NoContent()); }
public async Task <IActionResult> Get([FromRoute] string id, [FromQuery] string fields) { LoverAnniversary anniversary = await _anniversaryRepository.FindByIdAsync(id); if (anniversary == null) { return(NotFound()); } string userId = this.GetUserId(); if (!(anniversary.Lover?.HasUser(userId) ?? false)) { return(this.UserNoLoverResult(anniversary.Lover?.GetUser(userId))); } LoverAnniversaryResource anniversaryResource = _mapper.Map <LoverAnniversaryResource>(anniversary); ExpandoObject shapedAnniversaryResource = anniversaryResource.ToDynamicObject(fields); shapedAnniversaryResource.TryAdd("links", CreateLinksForAnniversary(anniversary.Id, fields)); return(Ok(shapedAnniversaryResource)); }
public async Task <IActionResult> Delete([FromRoute] string id) { LoverAnniversary anniversary = await _anniversaryRepository.FindByIdAsync(id); if (anniversary == null) { return(NotFound()); } var authorizationResult = await _authorizationService.AuthorizeAsync(User, anniversary, Operations.Delete); if (!authorizationResult.Succeeded) { return(Forbid()); } _anniversaryRepository.Delete(anniversary); if (!await _unitOfWork.SaveChangesAsync()) { throw new Exception("Failed to delete resource."); } return(NoContent()); }
public void Update(LoverAnniversary entity) { _dbContext.LoverAnniversaries.Update(entity); }
public void Delete(LoverAnniversary entity) { _dbContext.LoverAnniversaries.Remove(entity); }
public void Add(LoverAnniversary entity) { _dbContext.LoverAnniversaries.Add(entity); }