Example #1
0
        public async Task <IActionResult> PartiallyUpdate(
            [FromRoute] string id, [FromBody] JsonPatchDocument patchDoc)
        {
            LoverPhoto loverPhotoToUpdate = await _repository.FindByIdAsync(id);

            if (loverPhotoToUpdate == null)
            {
                return(NotFound());
            }

            var authorizationResult = await _authorizationService.AuthorizeAsync(
                User, loverPhotoToUpdate, Operations.Update);

            if (!authorizationResult.Succeeded)
            {
                return(Forbid());
            }

            LoverPhotoUpdateResource loverPhotoUpdateResource = _mapper.Map <LoverPhotoUpdateResource>(loverPhotoToUpdate);

            patchDoc.ApplyTo(loverPhotoUpdateResource);
            _mapper.Map(loverPhotoUpdateResource, loverPhotoToUpdate);

            if (!await _unitOfWork.SaveChangesAsync())
            {
                throw new Exception("保存数据失败");
            }
            return(NoContent());
        }
Example #2
0
        public async Task <IActionResult> Add([FromForm] LoverPhotoAddResource loverPhotoAddResource)
        {
            IFormFile file = loverPhotoAddResource.File;

            if (file == null)   // 表单中必须包含图片文件
            {
                ModelState.AddModelError("loverPhotoAddResource", $"parameter {file} cannot be null");
                return(BadRequest(ModelState));
            }

            // 无法自动映射表单的Tags到对应的Tags集合属性, 所以手动处理一下, 读取key为Tags的值, 反序列化json
            // 元数据是json数组, 示例: [{"name": "value"}, {"name", "value2"}]
            // 表单中只能有一个tags键
            Request.Form.TryGetValue("tags", out StringValues tagsStrings);
            if (tagsStrings.Count > 1)
            {
                return(BadRequest());
            }
            if (tagsStrings.Count <= 0)
            {
                tagsStrings = new StringValues("[]");
            }
            IList <TagAddResource> tags =
                JsonConvert.DeserializeObject <IList <TagAddResource> >(tagsStrings.FirstOrDefault());

            loverPhotoAddResource.Tags = tags;

            LoverCloudUser user = await _userRepository.FindByIdAsync(this.GetUserId());

            Lover lover = user.Lover;

            LoverPhoto loverPhoto = _mapper.Map <LoverPhoto>(loverPhotoAddResource);

            // 生成 PhotoPhysicalPath 要用到 Uploader, 所以先设置 Uploader 的值
            loverPhoto.Uploader     = user;
            loverPhoto.Lover        = lover;
            loverPhoto.PhysicalPath = loverPhoto.GeneratePhotoPhysicalPath(file.GetFileSuffix());;
            loverPhoto.UpdateDate   = DateTime.Now;
            loverPhoto.PhotoUrl     = Url.LinkRelative("GetPhoto", new { id = loverPhoto.Id });
            // 添加到数据库
            _repository.Add(loverPhoto);
            if (!await _unitOfWork.SaveChangesAsync())
            {
                throw new Exception("数据保存失败");
            }
            // 保存图片文件
            await file.SaveToFileAsync(loverPhoto.PhysicalPath);

            LoverPhotoResource loverPhotoResource = _mapper.Map <LoverPhotoResource>(loverPhoto);
            ExpandoObject      result             = loverPhotoResource.ToDynamicObject()
                                                    .AddLinks(this, null, "photo", "GetPhoto", "DeleteLoverPhoto", "PartiallyUpdateLoverPhoto");

            return(CreatedAtRoute("AddLoverPhoto", result));
        }
        public async Task <IActionResult> Add([FromForm] LoverLogAddResource addResource)
        {
            LoverCloudUser user = await _userRepository.FindByIdAsync(this.GetUserId());

            Lover lover = user.Lover;

            LoverLog loverLog = _mapper.Map <LoverLog>(addResource);

            loverLog.Creater        = user;
            loverLog.Lover          = lover;
            loverLog.CreateDateTime = DateTime.Now;
            loverLog.LastUpdateTime = DateTime.Now;
            _repository.Add(loverLog);
            if (addResource.Photos != null)
            {
                foreach (var formFile in addResource.Photos)
                {
                    var photo = new LoverPhoto
                    {
                        Name           = formFile.FileName,
                        Uploader       = user,
                        Lover          = lover,
                        LoverLog       = loverLog,
                        PhotoTakenDate = DateTime.Now,
                    };
                    photo.PhysicalPath = photo.GeneratePhotoPhysicalPath(formFile.GetFileSuffix());
                    loverLog.LoverPhotos.Add(photo);
                    await formFile.SaveToFileAsync(photo.PhysicalPath);
                }
            }

            if (!await _unitOfWork.SaveChangesAsync())
            {
                return(NoContent());
            }

            LoverLogResource loverLogResource       = _mapper.Map <LoverLogResource>(loverLog);
            ExpandoObject    shapedLoverLogResource = loverLogResource.ToDynamicObject()
                                                      .AddLinks(
                this, null, "log", "GetLoverLog",
                "DeleteLoverLog", "PartiallyUpdateLoverLog");

            return(CreatedAtRoute("AddLoverLog", shapedLoverLogResource));
        }
Example #4
0
        public async Task <IActionResult> Get([FromQuery] LoverAlbumParameters parameters)
        {
            PaginatedList <LoverAlbum> albums = await _albumRepository.GetLoverAlbumsAsync(this.GetUserId(), parameters);

            IQueryable <LoverAlbum> sortedAlbums = albums.AsQueryable()
                                                   .ApplySort(
                parameters.OrderBy,
                _propertyMappingContainer.Resolve <LoverAlbumResource, LoverAlbum>());

            IEnumerable <LoverAlbumResource> albumResources =
                _mapper.Map <IEnumerable <LoverAlbumResource> >(sortedAlbums);

            foreach (LoverAlbumResource source in albumResources)
            {
                source.PhotosCount = await _albumRepository.GetPhotosCount(source.Id);

                LoverPhoto converImage = await _albumRepository.GetCoverImage(source.Id);

                if (converImage != null)
                {
                    source.CoverImageUrl = Url.LinkRelative("GetPhoto", new { id = converImage.Id });
                }
            }

            IEnumerable <ExpandoObject> shapedAlbumResources = albumResources
                                                               .ToDynamicObject(parameters.Fields)
                                                               .AddLinks(
                this, parameters.Fields, "album",
                "GetLoverAlbum", "DeleteLoverAlbum", "PartiallyUpdateLoverAlbum");

            var result = new
            {
                value = shapedAlbumResources,
                links = this.CreatePaginationLinks("GetLoverAlbums", parameters, albums.HasPrevious, albums.HasNext)
            };

            this.AddPaginationHeaderToResponse(albums);

            return(Ok(result));
        }
Example #5
0
 public void Update(LoverPhoto entity)
 {
     _dbContext.Update(entity);
 }
Example #6
0
 public void Delete(LoverPhoto entity)
 {
     _dbContext.LoverPhotos.Remove(entity);
 }
Example #7
0
 public void Add(LoverPhoto entity)
 {
     _dbContext.LoverPhotos.Add(entity);
 }