public void PhotoInsert_WithObjects() { ThingEntity someThing; using (var context = new GalleryDbContext()) { someThing = context.Things.First(); } ObjectOnPhotoEntity thingOnPhoto = new ObjectOnPhotoEntity() { PositionX = 1, PositionY = 2, Id = Guid.NewGuid(), Object = someThing }; var detail = new PhotoDetailModel() { Name = "ThingsTestPhoto", ObjectsOnPhoto = { mapper.MapObjectOnPhotoEntityToModel(thingOnPhoto) } }; var photo = photoRepositorySUT.Insert(detail); Assert.NotNull(photo); using (var context = new GalleryDbContext()) { Assert.Contains(context.Photos, x => x.Id == photo.Id); } }
public PhotoDetailModel Update(PhotoDetailModel detail) { using (var galleryDbContext = new GalleryDbContext()) { var entity = galleryDbContext.Photos.First(r => r.Id == detail.Id); entity.CreationTime = detail.CreationTime; entity.Format = detail.Format; entity.Width = detail.Width; entity.Height = detail.Height; entity.Note = detail.Note; if (detail.Album != null) { var album = galleryDbContext.Albums.First(x => x.Id == detail.Album.Id); entity.Album = album; } foreach (var objectOnPhotoEntity in entity.ObjectsOnPhoto.ToList()) { galleryDbContext.ObjectOnPhotoEntities.Remove(objectOnPhotoEntity); galleryDbContext.SaveChanges(); } foreach (var objectOnPhotoModel in detail.ObjectsOnPhoto) { ObjectOnPhotoEntity toAdd = new ObjectOnPhotoEntity() { PositionX = objectOnPhotoModel.PositionX, PositionY = objectOnPhotoModel.PositionY, Id = Guid.NewGuid(), Photo = entity }; var thing = galleryDbContext.Things.FirstOrDefault(x => x.Id == objectOnPhotoModel.Object.Id); var person = galleryDbContext.Persons.FirstOrDefault(x => x.Id == objectOnPhotoModel.Object.Id); if (thing != null) { toAdd.Object = thing; } else if (person != null) { toAdd.Object = person; } galleryDbContext.ObjectOnPhotoEntities.Add(toAdd); } galleryDbContext.SaveChanges(); return(mapper.MapPhotoEntityToPhotoDetailModel(entity)); } }
public ObjectOnPhotoModel MapObjectOnPhotoEntityToModel(ObjectOnPhotoEntity entity) { return(new ObjectOnPhotoModel() { Id = entity.Id, Object = entity.Object, Photo = entity.Photo, PositionY = entity.PositionY, PositionX = entity.PositionX }); }