public static void Initialise(IServiceProvider serviceProvider)
        {
            using (var context = new RoomBookingsContext(serviceProvider.GetRequiredService <DbContextOptions <RoomBookingsContext> >()))
            {
                if (context.RoomTimes.Any())
                {
                    return;
                }

                context.RoomTimes.AddRange(
                    new RoomTime {
                    Id = 1, RoomId = 1, StartTime = new TimeSpan(8), EndTime = new TimeSpan(18)
                },
                    new RoomTime {
                    Id = 1, RoomId = 2, StartTime = new TimeSpan(0, 0, 7, 5), EndTime = new TimeSpan(18)
                },
                    new RoomTime {
                    Id = 1, RoomId = 3, StartTime = new TimeSpan(8), EndTime = new TimeSpan(16)
                },
                    new RoomTime {
                    Id = 1, RoomId = 4, StartTime = new TimeSpan(8), EndTime = new TimeSpan(18)
                }
                    );

                context.SaveChanges();
            }
        }
        public static void Initialise(IServiceProvider serviceProvider)
        {
            using (var context = new RoomBookingsContext(serviceProvider.GetRequiredService <DbContextOptions <RoomBookingsContext> >()))
            {
                if (context.People.Any())
                {
                    return;
                }

                context.People.AddRange(
                    new Person {
                    Id = 1, Name = "John Smith", DateOfBirth = new DateTime(2000, 12, 1, 12, 0, 0)
                },
                    new Person {
                    Id = 2, Name = "Jane Doe", DateOfBirth = new DateTime(1965, 12, 1, 12, 0, 0)
                },
                    new Person {
                    Id = 3, Name = "Jack Jones", DateOfBirth = new DateTime(1972, 12, 1, 12, 0, 0)
                },
                    new Person {
                    Id = 4, Name = "Mary Murphy", DateOfBirth = new DateTime(2001, 12, 1, 12, 0, 0)
                },
                    new Person {
                    Id = 5, Name = "Bob Trent", DateOfBirth = new DateTime(1983, 12, 1, 12, 0, 0)
                },
                    new Person {
                    Id = 6, Name = "John Smith", DateOfBirth = new DateTime(1999, 12, 1, 12, 0, 0)
                }
                    );

                context.SaveChanges();
            }
        }
Example #3
0
        // Helpers

        private async Task RunInContextAsync(Func <PeopleController, Task> func)
        {
            var dbOptions = new DbContextOptionsBuilder <RoomBookingsContext>().UseInMemoryDatabase(databaseName: "RoomBookings" + new Random().Next(1, 100000))
                            .Options;

            using (var context = new RoomBookingsContext(dbOptions))
            {
                var _mockPersonRepository = new Repository <Person>(context);
                var _mockPersonService    = new PersonService(_mockPersonRepository);
                var _mockPeopleController = new PeopleController(_mockPersonService);

                await func(_mockPeopleController);
            }
        }
Example #4
0
        public static void Initialise(IServiceProvider serviceProvider)
        {
            using (var context = new RoomBookingsContext(serviceProvider.GetRequiredService <DbContextOptions <RoomBookingsContext> >()))
            {
                if (context.Rooms.Any())
                {
                    return;
                }

                context.Rooms.AddRange(
                    new Room {
                    Id = 1, Name = "Room One"
                },
                    new Room {
                    Id = 2, Name = "Room Three"
                },
                    new Room {
                    Id = 3, Name = "Room Two"
                },
                    new Room {
                    Id = 4, Name = "Room Four"
                }
                    );

                context.SaveChanges();

                var roomTwo   = context.Rooms.FirstOrDefault(f => f.Id == 2);
                var roomThree = context.Rooms.FirstOrDefault(f => f.Id == 3);

                context.RoomBookings.AddRange(
                    new RoomBooking {
                    Id = 1, PersonId = 6, Room = roomTwo, StartTime = new DateTime(2021, 05, 20, 9, 45, 0), EndTime = new DateTime(2021, 05, 20, 10, 00, 0)
                },
                    new RoomBooking {
                    Id = 2, PersonId = 4, Room = roomThree, StartTime = new DateTime(2021, 05, 20, 10, 15, 0), EndTime = new DateTime(2021, 05, 20, 11, 15, 0)
                },
                    new RoomBooking {
                    Id = 3, PersonId = 1, Room = roomThree, StartTime = new DateTime(2021, 05, 20, 9, 00, 0), EndTime = new DateTime(2021, 05, 20, 10, 00, 0)
                }

                    );

                context.SaveChanges();
            }
        }
Example #5
0
        protected override ValidationResult?IsValid(object?value, ValidationContext validationContext)
        {
            var className     = validationContext.ObjectType.Name.Split('.').Last();
            var propertyName  = validationContext.MemberName;
            var parameterName = string.Format("@{0}", propertyName);

            using var context = new RoomBookingsContext();

            var result = context.People.FromSqlRaw(string.Format("SELECT COUNT(*) FROM {0} WHERE {1}={2}",
                                                                 className, propertyName, parameterName)).ToList();

            //if (result[0] > 0)
            //{
            //    return new ValidationResult(string.Format("The '{0}' already exist", propertyName),
            //        new List<string>() { propertyName });
            //}

            return(null);
        }
 public RoomService(RoomBookingsContext repository)
 {
     _repository = repository;
 }
 public RoomService(RoomBookingsContext context, IMapper mapper)
 {
     _context = context;
     _mapper  = mapper;
 }