コード例 #1
0
        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));
        }
コード例 #2
0
        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));
        }