public async Task <IActionResult> PartiallyUpdate(
            [FromRoute] string id, [FromBody] JsonPatchDocument <LoverLogUpdateResource> patchDoc)
        {
            LoverLog loverLog = await _repository.FindByIdAsync(id);

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

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

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

            LoverLogUpdateResource loverLogUpdateResource = _mapper.Map <LoverLogUpdateResource>(loverLog);

            patchDoc.ApplyTo(loverLogUpdateResource);
            _mapper.Map(loverLogUpdateResource, loverLog);
            loverLog.LastUpdateTime = DateTime.Now;

            if (!await _unitOfWork.SaveChangesAsync())
            {
                throw new Exception("Failed to update lover log resource");
            }
            return(NoContent());
        }
        public async Task <IActionResult> Delete([FromRoute] string id)
        {
            LoverLog loverLog = await _repository.FindByIdAsync(id);

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

            var authorizationResult = await _authorizationService.AuthorizeAsync(
                User, loverLog, Operations.Delete);

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

            _repository.Delete(loverLog);
            if (!await _unitOfWork.SaveChangesAsync())
            {
                throw new Exception("Failed to delete resource");
            }

            return(NoContent());
        }
        public void Delete(LoverLog entity)
        {
            var photos = _dbContext.LoverPhotos.Where(x => x.LoverLog.Id == entity.Id);

            foreach (LoverPhoto item in photos)
            {
                _dbContext.LoverPhotos.Remove(item);
            }
            _dbContext.LoverLogs.Remove(entity);
        }
        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));
        }
        public async Task <IActionResult> Get([FromRoute] string id, [FromQuery] string fields)
        {
            LoverLog loverLog = await _repository.FindByIdAsync(id);

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

            string userId = this.GetUserId();

            if (!(loverLog?.Lover?.LoverCloudUsers.Any(user => user.Id == userId) ?? false))
            {
                return(Forbid());
            }

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

            return(Ok(shapedLoverLogResource));
        }
 public void Update(LoverLog entity)
 {
     _dbContext.LoverLogs.Update(entity);
 }
 public void Add(LoverLog entity)
 {
     _dbContext.LoverLogs.Add(entity);
 }