public async Task <IActionResult> PartiallyUpdate( [FromRoute] string id, [FromBody] JsonPatchDocument <LoverAlbumUpdateResource> patchDoc) { LoverAlbum album = await _albumRepository.FindByIdAsync(id); if (album == null) { return(NotFound()); } var authorizationResult = await _authorizationService.AuthorizeAsync(User, album, Operations.Update); if (!authorizationResult.Succeeded) { return(Forbid()); } LoverAlbumUpdateResource loverAlbumUpdateResource = _mapper.Map <LoverAlbumUpdateResource>(album); patchDoc.ApplyTo(loverAlbumUpdateResource); _mapper.Map(loverAlbumUpdateResource, album); album.LastUpdate = DateTime.Now; if (!await _unitOfWork.SaveChangesAsync()) { throw new Exception($"Failed to update album, id: {album.Id}"); } return(NoContent()); }
public async Task <IActionResult> Delete(string id) { LoverAlbum albumToDelete = await _albumRepository.FindByIdAsync(id); if (albumToDelete == null) { return(NotFound()); } var authorizationResult = await _authorizationService.AuthorizeAsync(User, albumToDelete, Operations.Delete); if (!authorizationResult.Succeeded) { return(Forbid()); } _albumRepository.Delete(albumToDelete); if (!await _unitOfWork.SaveChangesAsync()) { throw new Exception($"Failed to delete album {id}, {albumToDelete.Name}"); } return(NoContent()); }
public async Task <IActionResult> Add([FromBody] LoverAlbumAddResource addResource) { if (!ModelState.IsValid) { return(UnprocessableEntity(ModelState)); } LoverCloudUser user = await GetUserAsync(); //if (user.Lover == null) return UserNoLoverResult(user); LoverAlbum album = _mapper.Map <LoverAlbum>(addResource); album.Creater = user; album.Lover = user.Lover; _albumRepository.Add(album); if (!await _unitOfWork.SaveChangesAsync()) { throw new Exception("Failed to save changes"); } LoverAlbumResource albumResource = _mapper.Map <LoverAlbumResource>(album); ExpandoObject shapedAlbumResource = albumResource.ToDynamicObject() .AddLinks(this, null, "album", "GetLoverAlbum", "DeleteLoverAlbum", "PartiallyUpdateLoverAlbum"); return(CreatedAtRoute("AddLoverAlbum", shapedAlbumResource)); }
public async Task <IActionResult> Get([FromRoute] string id, [FromQuery] string fields) { LoverAlbum album = await _albumRepository.FindByIdAsync(id); // 确保该资源属于当前登录的用户 if (!album?.Lover.LoverCloudUsers.Any(user => user.Id == this.GetUserId()) ?? false) { return(Forbid()); } if (album == null) { return(NotFound()); } LoverAlbumResource albumResource = _mapper.Map <LoverAlbumResource>(album); albumResource.PhotosCount = await _albumRepository.GetPhotosCount(albumResource.Id); ExpandoObject shapedAlbumResource = albumResource.ToDynamicObject(fields) .AddLinks(this, fields, "album", "GetLoverAlbum", "DeleteLoverAlbum", "PartiallyUpdateLoverAlbum"); return(Ok(shapedAlbumResource)); }
public void Update(LoverAlbum entity) { _dbContext.LoverAlbums.Update(entity); }
public void Delete(LoverAlbum entity) { _dbContext.LoverAlbums.Remove(entity); }
public void Add(LoverAlbum entity) { _dbContext.LoverAlbums.Add(entity); }