public async Task <IActionResult> PostPersonPhoto([FromBody] PersonPhoto personPhoto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.PersonPhoto.Add(personPhoto);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (PersonPhotoExists(personPhoto.HashPhoto))
                {
                    return(new StatusCodeResult(StatusCodes.Status409Conflict));
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetPersonPhoto", new { id = personPhoto.HashPhoto }, personPhoto));
        }
예제 #2
0
        private static async Task InsertTestData(IServiceProvider serviceProvider)
        {
            var context = serviceProvider.GetService<StoreDbContext>();

            var photos = new List<Photo> { new Photo { Id = 1, Name = "Photo 1"}, new Photo { Id = 2, Name = "Photo 2" } };
            ////context.Photos.AddRange(photos);
            foreach (var photo in photos)
            {
                context.Entry(photo).State = EntityState.Added;
            }

            await context.SaveChangesAsync();

            var person = new Person { Id = 1, Name = "Je_Kl" };
            ////var persons = new List<Person> { person };
            ////context.Persons.AddRange(persons);
            context.Entry(person).State = EntityState.Added;
            await context.SaveChangesAsync();

            foreach (var photo in photos)
            {
                var personPhoto = new PersonPhoto { PersonId = person.Id, PhotoId = photo.Id };
                context.Entry(personPhoto).State = EntityState.Added;
            }
            await context.SaveChangesAsync();

            var photos2 = new List<Photo> { new Photo { Id = 3, Name = "Photo 3" }, new Photo { Id = 4, Name = "Photo 4" } };
            foreach (var photo in photos2)
            {
                context.Entry(photo).State = EntityState.Added;
            }
            await context.SaveChangesAsync();
        }
예제 #3
0
        public async Task <PersonPhoto> CreateAsync(PersonPhoto personPhoto)
        {
            var command = "INSERT INTO public.\"PersonPhoto\" " +
                          " (" +
                          $"\"{nameof(PersonPhoto.Id)}\", " +
                          $"\"{nameof(PersonPhoto.UserId)}\"," +
                          $"\"{nameof(PersonPhoto.FileName)}\", " +
                          $"\"{nameof(PersonPhoto.CreationDate)}\", " +
                          $"\"{nameof(PersonPhoto.LastModified)}\")" +
                          "VALUES (@Id, @UserId, @FileName, @CreationDate, @LastModified);";

            int rowsInserted;

            using (var sqlConnection = await _context.CreateConnectionAsync())
            {
                rowsInserted = await sqlConnection.ExecuteAsync(command, new
                {
                    personPhoto.Id,
                    personPhoto.UserId,
                    personPhoto.FileName,
                    personPhoto.CreationDate,
                    personPhoto.LastModified
                });
            }

            if (rowsInserted != 1)
            {
                throw new ApplicationApiException(HttpStatusCode.BadRequest, $"Unable to create {nameof(PersonPhoto)} object");
            }

            return(personPhoto);
        }
        public static ActionResult Tag(BaseController controller, int id, int?personId = null, int?showId = null)
        {
            if (id == Photo.NoPic)
            {
                controller.RollbackTransactionFast();
                return(new HttpBadRequestResult("Cannot tag this photo."));
            }

            if (personId.HasValue && !controller.DatabaseSession.Query <PersonPhoto>().Any(x => x.Photo.PhotoId == id && x.Person.PersonId == personId.Value))
            {
                var personPhoto = new PersonPhoto();
                personPhoto.Person           = controller.DatabaseSession.Load <Person>(personId.Value);
                personPhoto.Photo            = controller.DatabaseSession.Load <Photo>(id);
                personPhoto.InsertedDateTime = DateTime.UtcNow;
                controller.DatabaseSession.Save(personPhoto);
            }
            if (showId.HasValue && !controller.DatabaseSession.Query <ShowPhoto>().Any(x => x.Photo.PhotoId == id && x.Show.ShowId == showId.Value))
            {
                var showPhoto = new ShowPhoto();
                showPhoto.Show             = controller.DatabaseSession.Load <Show>(showId.Value);
                showPhoto.Photo            = controller.DatabaseSession.Load <Photo>(id);
                showPhoto.InsertedDateTime = DateTime.UtcNow;
                controller.DatabaseSession.Save(showPhoto);
            }

            return(null);
        }
        public async Task <IActionResult> PutPersonPhoto([FromRoute] long id, [FromBody] PersonPhoto personPhoto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != personPhoto.HashPhoto)
            {
                return(BadRequest());
            }

            _context.Entry(personPhoto).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PersonPhotoExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
예제 #6
0
        public virtual async Task <int> AddPhotoAsync(PersonPhoto photo)
        {
            if (photo is null)
            {
                throw new NullReferenceException("Photo data cannot be empty");
            }
            if (!_context.People.Any(e => e.Id == photo.PersonId))
            {
                throw new ArgumentException("Person is not specified");
            }
            if (string.IsNullOrEmpty(photo.Name))
            {
                throw new ArgumentException("Photo name has not passed");
            }
            if (!new Regex("[^-A-Za-z0-9+/=]|=[^=]|={2,}$").IsMatch(photo.Base64))
            {
                throw new ArgumentException("Base 64 is broken");
            }

            bool parsed = new FileExtensionContentTypeProvider().TryGetContentType(photo.Name, out string mime);

            if (!parsed)
            {
                throw new ArgumentException("Unknown file MIME");
            }

            photo.Mime      = mime;
            photo.CreatedOn = _dateTimeUtil.GetCurrentDateTime();
            _context.Photos.Add(photo);
            return(await _context.SaveChangesAsync());
        }
예제 #7
0
        public void Setup()
        {
            InitializeMocks();
            InitializeLoggerMock(new PeopleService(null, null, null));

            int idPersonOne = 1;

            _contact = new Contact
            {
                PersonId    = idPersonOne,
                CreatedOn   = _dateTimeUtil.GetCurrentDateTime(),
                Id          = 1,
                ContactType = Enums.ContactType.Phone,
                Name        = "test",
                Value       = "test"
            };
            _dbContext.Contacts.Add(_contact);

            _photo = new PersonPhoto
            {
                Id        = 1,
                Mime      = "test",
                Base64    = "test",
                CreatedOn = _dateTimeUtil.GetCurrentDateTime(),
                Name      = "test",
                PersonId  = idPersonOne
            };
            _dbContext.Photos.Add(_photo);

            _person1 = new Person
            {
                Id         = idPersonOne,
                LastName   = "Test",
                Name       = "Test",
                SecondName = "Test",
                BornedOn   = _dateTimeUtil.GetCurrentDateTime(),
                CreatedOn  = _dateTimeUtil.GetCurrentDateTime(),
                Contacts   = new List <Contact> {
                    _contact
                },
                Photos = new List <PersonPhoto> {
                    _photo
                }
            };
            _person2 = new Person
            {
                Id         = 2,
                LastName   = "Test",
                Name       = "Test",
                SecondName = "Test",
                BornedOn   = _dateTimeUtil.GetCurrentDateTime(),
                CreatedOn  = _dateTimeUtil.GetCurrentDateTime(),
            };

            _dbContext.People.Add(_person1);
            _dbContext.People.Add(_person2);

            _peopleService = new PeopleService(_peopleRepository, _logger, _dateTimeUtil);
        }
예제 #8
0
 public PersonDataView()
 {
     InitializeComponent();
     LoadPhotoUpdater();
     LoadPerson(new PersonalData());
     _gfx = PersonPhoto.CreateGraphics();
     _gfx.CompositingMode    = CompositingMode.SourceOver;
     _gfx.CompositingQuality = CompositingQuality.HighSpeed;
     _gfx.InterpolationMode  = InterpolationMode.Low;
     _gfx.PixelOffsetMode    = PixelOffsetMode.HighSpeed;
     _gfx.SmoothingMode      = SmoothingMode.HighSpeed;
 }
        protected override void Context()
        {
            base.Context();
            photo = new PersonPhoto {
                Image = Encoding.Default.GetBytes("This is a placeholder for a photo...")
            };
            person = new Person("Schenker", "Gabriel", photo);
            Session.Save(person);

            Session.Flush();
            Session.Clear();
        }
예제 #10
0
        private static async Task InsertTestData(IServiceProvider serviceProvider)
        {
            var context = serviceProvider.GetService <StoreDbContext>();

            var photos = new List <Photo> {
                new Photo {
                    Id = 1, Name = "Photo 1"
                }, new Photo {
                    Id = 2, Name = "Photo 2"
                }
            };

            ////context.Photos.AddRange(photos);
            foreach (var photo in photos)
            {
                context.Entry(photo).State = EntityState.Added;
            }

            await context.SaveChangesAsync();

            var person = new Person {
                Id = 1, Name = "Je_Kl"
            };

            ////var persons = new List<Person> { person };
            ////context.Persons.AddRange(persons);
            context.Entry(person).State = EntityState.Added;
            await context.SaveChangesAsync();

            foreach (var photo in photos)
            {
                var personPhoto = new PersonPhoto {
                    PersonId = person.Id, PhotoId = photo.Id
                };
                context.Entry(personPhoto).State = EntityState.Added;
            }
            await context.SaveChangesAsync();

            var photos2 = new List <Photo> {
                new Photo {
                    Id = 3, Name = "Photo 3"
                }, new Photo {
                    Id = 4, Name = "Photo 4"
                }
            };

            foreach (var photo in photos2)
            {
                context.Entry(photo).State = EntityState.Added;
            }
            await context.SaveChangesAsync();
        }
예제 #11
0
        public IActionResult PhotoTag(PhotoTagViewModel viewModel)
        {
            var personPhoto = new PersonPhoto
            {
                PersonId = viewModel.PersonId,
                PhotoId  = viewModel.PhotoId
            };

            Db.Insert(personPhoto);

            viewModel.People   = this.AllPeople;
            viewModel.Photos   = this.AllPhotos;
            viewModel.PersonId = 0;
            viewModel.PhotoId  = 0;

            return(View(viewModel));
        }
        private static void CreateTestPersonWithPhotos(DbContext context)
        {
            var photos = new List <Photo> {
                new Photo {
                    Id = 1, Name = "Photo 1"
                }, new Photo {
                    Id = 2, Name = "Photo 2"
                }
            };

            foreach (var photo in photos)
            {
                context.Entry(photo).State = EntityState.Added;
            }
            context.SaveChanges();

            var person = new Person {
                Id = 1, Name = "Je_Kl"
            };

            context.Entry(person).State = EntityState.Added;
            context.SaveChanges();

            foreach (var photo in photos)
            {
                var personPhoto = new PersonPhoto {
                    PersonId = person.Id, PhotoId = photo.Id
                };
                context.Entry(personPhoto).State = EntityState.Added;
            }
            context.SaveChanges();

            var photos2 = new List <Photo> {
                new Photo {
                    Id = 3, Name = "Photo 3"
                }, new Photo {
                    Id = 4, Name = "Photo 4"
                }
            };

            foreach (var photo in photos2)
            {
                context.Entry(photo).State = EntityState.Added;
            }
            context.SaveChanges();
        }
예제 #13
0
        public void AddPhotoAsync_should_add_photo_entity_to_db()
        {
            // Arrange
            PersonPhoto personPhoto = new()
            {
                PersonId = _person1.Id,
                Name     = "1.jpg",
                Base64   = "dGVzdCBmaWxlIG9uZQ=="
            };

            // Act
            int         result = _peopleRepository.AddPhotoAsync(personPhoto).Result;
            PersonPhoto actual = _dbContext.Photos.FirstOrDefault(e => e.Id == personPhoto.Id);

            // Assert
            Assert.AreEqual(personPhoto, actual, "Person photo added to db as expected");
            _dbContextMock.Verify(a => a.SaveChangesAsync(true, new CancellationToken()), Times.Once);
        }
예제 #14
0
        public PersonPhotoViewModel(PersonPhoto pp)
        {
            if (pp != null)
            {
                if (pp.Person != null)
                {
                    this.PersonId = pp.Person.Id;
                }

                if (pp.Photo != null)
                {
                    this.PhotoId   = pp.Photo.Id;
                    this.PhotoName = pp.Photo.PhotoName;
                }

                this.Archive = pp.Archive;
                this.Notes   = pp.Notes;
            }
        }
        public ActionResult Upload(int personId, PhotosController.UploadPOSTParameters param)
        {
            var validationResult = param.Validate();

            if (validationResult != null)
            {
                return(validationResult);
            }

            var photo = PhotosController.Upload(this, param);

            var personPhoto = new PersonPhoto();

            personPhoto.Person           = DatabaseSession.Load <Person>(personId);
            personPhoto.Photo            = photo;
            personPhoto.InsertedDateTime = DateTime.UtcNow;
            DatabaseSession.Save(personPhoto);

            return(Redirect(Url.GetUrl(ListPersonPhotos, personId, (int?)photo.PhotoId)));
        }
예제 #16
0
        public async Task <PersonPhoto> UpdateAsync(PersonPhoto personPhoto)
        {
            var command = "UPDATE public.\"PersonPhoto\" " +
                          "SET" +
                          $"\"{nameof(PersonPhoto.FileName)}\" = @FileName, " +
                          $"\"{nameof(PersonPhoto.LastModified)}\" = @LastModified" +
                          $" WHERE \"{nameof(PersonPhoto.UserId)}\" = @UserId;";

            using (var sqlConnection = await _context.CreateConnectionAsync())
            {
                await sqlConnection.ExecuteAsync(command, new
                {
                    personPhoto.FileName,
                    personPhoto.LastModified,
                    personPhoto.UserId
                });
            }

            return(personPhoto);
        }
예제 #17
0
        private static void ImportPersonPhotos()
        {
            var media = oldDatabaseConnection.Query("SELECT * FROM media").ToList();

            Log("Importing " + media.Count + " person media");

            using (var session = sessionFactory.OpenSession())
            {
                session.Transaction.Begin();
                session.CreateSQLQuery("SET IDENTITY_INSERT [dbo].PersonPhoto ON;").ExecuteUpdate();
                var maxId = 0;
                foreach (var _row in media)
                {
                    if (_row.IDtype.ToLower() != "people")
                    {
                        continue;
                    }
                    var entity = new PersonPhoto();
                    entity.PersonPhotoId    = _row.ID;
                    entity.Person           = session.Load <Person>(_row.assocID);
                    entity.Photo            = session.Load <Photo>(_row.item_id);
                    entity.InsertedDateTime = DateTime.MinValue;
                    if (_row.last_mod != null)
                    {
                        entity.InsertedDateTime = TimeZoneInfo.ConvertTimeToUtc(_row.last_mod, TimeZoneCode.Eastern.ToTimeZoneInfo());
                    }
                    session.Save(entity, entity.PersonPhotoId);
                    if (entity.PersonPhotoId > maxId)
                    {
                        maxId = entity.PersonPhotoId;
                    }
                }
                session.Flush();
                session.CreateSQLQuery("SET IDENTITY_INSERT [dbo].PersonPhoto OFF;").ExecuteUpdate();
                session.CreateSQLQuery("DBCC CHECKIDENT ('dbo.PersonPhoto', RESEED, " + (maxId + 1) + ")").ExecuteUpdate();
                session.Transaction.Commit();
                session.Close();
            }
        }
        public async Task <IActionResult> Edit([Bind("Id,Name")] PersonViewModel model, params string[] selectedPhotos)
        {
            var pers = await this.dbContext.Persons.Include(f => f.PersonPhotos).AsNoTracking().FirstAsync(p => p.Id == model.Id);

            if (pers == null)
            {
                return(this.HttpNotFound());
            }

            if (this.ModelState.IsValid)
            {
                pers.Name = model.Name;

                this.dbContext.Entry(pers).State = EntityState.Modified;
                this.dbContext.SaveChanges();

                selectedPhotos = selectedPhotos ?? new string[] { };

                // Remove all Photos from Person
                pers.PersonPhotos.ToList().ForEach(pp => dbContext.Entry(pp).State = EntityState.Deleted);

                this.dbContext.SaveChanges();

                var newFotos = this.dbContext.Set <Photo>().Where(p => selectedPhotos.Any(n => n == p.Name));
                foreach (var foto in newFotos)
                {
                    var personPhoto = new PersonPhoto {
                        PersonId = pers.Id, PhotoId = foto.Id
                    };
                    this.dbContext.Entry(personPhoto).State = EntityState.Added;
                }
                this.dbContext.SaveChanges();

                return(this.RedirectToAction("Index"));
            }

            return(this.View(model));
        }
예제 #19
0
        public void Setup()
        {
            InitializeMocks();
            DbContextMock.ShouldThrowException = false;

            _contact1 = new Contact
            {
                Id          = 1,
                Name        = "Personal Phone",
                Value       = "123123123",
                CreatedOn   = _dateTimeUtil.GetCurrentDateTime(),
                ContactType = Enums.ContactType.Phone,
                PersonId    = 1,
                Person      = _person1
            };

            _contact2 = new Contact
            {
                Id          = 2,
                Name        = "Personal Phone",
                Value       = "123123124",
                CreatedOn   = _dateTimeUtil.GetCurrentDateTime().AddDays(1),
                ContactType = Enums.ContactType.Phone,
                PersonId    = 1,
                Person      = _person1
            };

            _contact3 = new Contact
            {
                Id          = 2,
                Name        = "Work Email",
                Value       = "*****@*****.**",
                CreatedOn   = _dateTimeUtil.GetCurrentDateTime(),
                ContactType = Enums.ContactType.Email,
                PersonId    = 1,
                Person      = _person1
            };

            _dbContext.Contacts.Add(_contact1);
            _dbContext.Contacts.Add(_contact2);
            _dbContext.Contacts.Add(_contact3);

            _photo1 = new PersonPhoto
            {
                Id        = 1,
                PersonId  = 1,
                Person    = _person1,
                CreatedOn = _dateTimeUtil.GetCurrentDateTime(),
                Mime      = "image/png",
                Base64    = "ejJlQUVFSFpCcUpjVDNlWW5WcEd0QQ=="
            };

            _photo2 = new PersonPhoto
            {
                Id        = 2,
                PersonId  = 1,
                Person    = _person1,
                CreatedOn = _dateTimeUtil.GetCurrentDateTime().AddDays(1),
                Mime      = "image/png",
                Base64    = "ejJlQUVFSFpCcUpjVDNlWW5WcEd0QQ=="
            };

            _dbContext.Photos.Add(_photo1);
            _dbContext.Photos.Add(_photo2);

            _person1 = new Person
            {
                Id         = 1,
                Name       = "Jonh",
                LastName   = "Jonhson",
                SecondName = "Peter",
                CreatedOn  = _dateTimeUtil.GetCurrentDateTime(),
                BornedOn   = _dateTimeUtil.GetCurrentDateTime(),
                Contacts   = new List <Contact> {
                    _contact1, _contact2, _contact3
                },
                Photos = new List <PersonPhoto> {
                    _photo1, _photo2
                }
            };
            _dbContext.People.Add(_person1);

            _peopleRepository = new PeopleRepository(_dbContext, _dateTimeUtil);
        }
예제 #20
0
        public static ActionResult Tag(BaseController controller, int id, int? personId = null, int? showId = null)
        {
            if (id == Photo.NoPic)
            {
                controller.RollbackTransactionFast();
                return new HttpBadRequestResult("Cannot tag this photo.");
            }

            if (personId.HasValue && !controller.DatabaseSession.Query<PersonPhoto>().Any(x => x.Photo.PhotoId == id && x.Person.PersonId == personId.Value))
            {
                var personPhoto = new PersonPhoto();
                personPhoto.Person = controller.DatabaseSession.Load<Person>(personId.Value);
                personPhoto.Photo = controller.DatabaseSession.Load<Photo>(id);
                personPhoto.InsertedDateTime = DateTime.UtcNow;
                controller.DatabaseSession.Save(personPhoto);
            }
            if (showId.HasValue && !controller.DatabaseSession.Query<ShowPhoto>().Any(x => x.Photo.PhotoId == id && x.Show.ShowId == showId.Value))
            {
                var showPhoto = new ShowPhoto();
                showPhoto.Show = controller.DatabaseSession.Load<Show>(showId.Value);
                showPhoto.Photo = controller.DatabaseSession.Load<Photo>(id);
                showPhoto.InsertedDateTime = DateTime.UtcNow;
                controller.DatabaseSession.Save(showPhoto);
            }

            return null;
        }
        public async Task<IActionResult> Edit([Bind("Id,Name")] PersonViewModel model, params string[] selectedPhotos)
        {
            var pers = await this.dbContext.Persons.Include(f => f.PersonPhotos).AsNoTracking().FirstAsync(p => p.Id == model.Id);
            if (pers == null)
            {
                return this.HttpNotFound();
            }

            if (this.ModelState.IsValid)
            {
                pers.Name = model.Name;
                
                this.dbContext.Entry(pers).State = EntityState.Modified;
                this.dbContext.SaveChanges();

                selectedPhotos = selectedPhotos ?? new string[] { };

                // Remove all Photos from Person
                pers.PersonPhotos.ToList().ForEach(pp => dbContext.Entry(pp).State = EntityState.Deleted);

                this.dbContext.SaveChanges();

                var newFotos = this.dbContext.Set<Photo>().Where(p => selectedPhotos.Any(n => n == p.Name));
                foreach (var foto in newFotos)
                {
                    var personPhoto = new PersonPhoto {PersonId = pers.Id, PhotoId = foto.Id};
                    this.dbContext.Entry(personPhoto).State = EntityState.Added;
                }
                this.dbContext.SaveChanges();

                return this.RedirectToAction("Index");
            }

            return this.View(model);
        }
        private static void CreateTestPersonWithPhotos(DbContext context)
        {
            var photos = new List<Photo> { new Photo { Id = 1, Name = "Photo 1" }, new Photo { Id = 2, Name = "Photo 2" } };
            foreach (var photo in photos)
            {
                context.Entry(photo).State = EntityState.Added;
            }
            context.SaveChanges();

            var person = new Person { Id = 1, Name = "Je_Kl" };
            context.Entry(person).State = EntityState.Added;
            context.SaveChanges();

            foreach (var photo in photos)
            {
                var personPhoto = new PersonPhoto { PersonId = person.Id, PhotoId = photo.Id };
                context.Entry(personPhoto).State = EntityState.Added;
            }
            context.SaveChanges();

            var photos2 = new List<Photo> { new Photo { Id = 3, Name = "Photo 3" }, new Photo { Id = 4, Name = "Photo 4" } };
            foreach (var photo in photos2)
            {
                context.Entry(photo).State = EntityState.Added;
            }
            context.SaveChanges();
        }