Пример #1
0
 public PositionInPictureModel Insert(PositionInPictureModel item)
 {
     using (var context = new MyPhotoDbContext())
     {
         var entity = _mapper.MapToEntity(item);
         entity.Id = Guid.NewGuid();
         context.PositionsInPicture.Add(entity);
         context.SaveChanges();
         return(_mapper.MapToModel(entity));
     }
 }
Пример #2
0
        public void Update(PictureDetailModel item)
        {
            using (var context = new MyPhotoDbContext())
            {
                var entity = context.Pictures.Include(x => x.PictureObjectCollection).First(picture => picture.Id == item.Id);
                entity.Name             = item.Name;
                entity.Description      = item.Description;
                entity.Format           = item.Format;
                entity.PhotoTakenDate   = item.PhotoTakenDate;
                entity.ResolutionHeight = item.ResolutionHeight;
                entity.ResolutionWidth  = item.ResolutionWidth;
                foreach (var position in item.PositionCollection)
                {
                    if (position.Id == Guid.Empty || context.PositionsInPicture.Include(x => x.PictureObject).FirstOrDefault(x => x.Id == position.Id) == null)
                    {
                        var positionInPictureMapper = new PositionInPictureMapper();
                        var helper = position.PictureObject;

                        if (position.Id == Guid.Empty)
                        {
                            position.Id = Guid.NewGuid();
                        }

                        position.PictureObject = null;
                        entity.PictureObjectCollection.Add(positionInPictureMapper.MapToEntity(position));
                        var personObject = context.Persons.FirstOrDefault(x => x.Id == helper.Id);
                        if (personObject == null)
                        {
                            var objectInPicture = context.Objects.Find(helper.Id);
                            if (objectInPicture != null)
                            {
                                entity.PictureObjectCollection.First(x => x.Id == position.Id).PictureObject = objectInPicture;
                            }
                        }
                        else
                        {
                            entity.PictureObjectCollection.First(x => x.Id == position.Id).PictureObject = personObject;
                        }
                    }
                    else
                    {
                        _positionInPictureRepository.Update(position);
                    }
                }

                var removeCollecction = new List <PositionInPictureEntity>();
                foreach (var positionInPictureEntity in entity.PictureObjectCollection)
                {
                    if (item.PositionCollection.FirstOrDefault(x => x.Id == positionInPictureEntity.Id) == null)
                    {
                        removeCollecction.Add(positionInPictureEntity);
                    }
                }

                foreach (var positionInPictureEntity in removeCollecction)
                {
                    entity.PictureObjectCollection.Remove(positionInPictureEntity);
                }
                context.SaveChanges();
            }
        }