コード例 #1
0
ファイル: Startup.cs プロジェクト: jsheridanwells/Landon.Api
        private static void AddTestData(
            HotelApiContext context,
            IDateLogicService dateLogicService)
        {
            context.Rooms.Add(new RoomEntity
            {
                Id   = Guid.Parse("ee2b83be-91db-4de5-8122-35a9e9195976"),
                Name = "Driscoll Suite",
                Rate = 23959
            });

            var oxford = context.Rooms.Add(new RoomEntity
            {
                Id   = Guid.Parse("301df04d-8679-4b1b-ab92-0a586ae53d08"),
                Name = "Oxford Suite",
                Rate = 10119,
            }).Entity;

            var today = DateTimeOffset.Now;
            var start = dateLogicService.AlignStartTime(today);
            var end   = start.Add(dateLogicService.GetMinimumStay());

            context.Bookings.Add(new BookingEntity
            {
                Id        = Guid.Parse("2eac8dea-2749-42b3-9d21-8eb2fc0fd6bd"),
                Room      = oxford,
                CreatedAt = DateTimeOffset.UtcNow,
                StartAt   = start,
                EndAt     = end,
                Total     = oxford.Rate,
            });

            context.SaveChanges();
        }
コード例 #2
0
        public static void AddTestData(HotelApiContext dbContext, IDateLogicService dateLogicService)
        {
            var premium = dbContext.Rooms.Add(new RoomEntity()
            {
                Id   = Guid.Parse("5c8fbfee-e9e3-4bcc-bdee-643203a30ef9"),
                Name = "Premium Suite",
                Rate = 10119
            }).Entity;

            dbContext.Rooms.Add(new RoomEntity()
            {
                Id   = Guid.Parse("c3b7ed37-e6a9-457c-b1c8-42cc7ba07095"),
                Name = "Standard",
                Rate = 23959
            });

            var today = DateTimeOffset.Now;
            var start = dateLogicService.AlignStartTime(today);
            var end   = start.Add(dateLogicService.GetMinimumStay());

            dbContext.Bookings.Add(new BookingEntity
            {
                Id        = Guid.Parse("2eac8dea-2749-42b3-9d21-8eb2fc0fd6bd"),
                Room      = premium,
                CreatedAt = DateTimeOffset.UtcNow,
                StartAt   = start,
                EndAt     = end,
                Total     = premium.Rate,
            });

            dbContext.SaveChanges();
        }
コード例 #3
0
        public async Task<ActionResult> CreateBookingForRoom(
            Guid roomId, [FromBody] BookingForm bookingForm)
        {
            var room = await _roomService.GetRoomAsync(roomId);
            if (room == null) return NotFound();

            var minimumStay = _dateLogicService.GetMinimumStay();
            bool tooShort = (bookingForm.EndAt.Value - bookingForm.StartAt.Value) < minimumStay;
            if (tooShort) return BadRequest(new ApiError(
                $"The minimum booking duration is {minimumStay.TotalHours} hours."));

            var conflictedSlots = await _openingService.GetConflictingSlots(
                roomId, bookingForm.StartAt.Value, bookingForm.EndAt.Value);
            if (conflictedSlots.Any()) return BadRequest(new ApiError(
                "This time conflicts with an existing booking."));

            // Get the current user (TODO)
            var userId = Guid.NewGuid();

            var bookingId = await _bookingService.CreateBookingAsync(
                userId, roomId, bookingForm.StartAt.Value, bookingForm.EndAt.Value);

            return Created(
                Url.Link(nameof(BookingsController.GetBookingById),
                new { bookingId }),
                null);
        }
コード例 #4
0
        public async Task <ActionResult> CreateBookingForRoom(Guid roomId, [FromBody] BookingForm bookingForm) //object je bindovany z body POST requestu, asp.net core robi model validaciu automaticky
        {
            var room = await _roomService.GetRoomAsync(roomId);

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

            var  minimumStay = _dateLogicService.GetMinimumStay();
            bool tooShort    = (bookingForm.EndAt.Value - bookingForm.StartAt.Value) < minimumStay;

            if (tooShort)
            {
                return(BadRequest(new ApiError($"The minimum booking duration is {minimumStay.TotalHours} hours.")));
            }

            var conflictedSlots = await _openingService.GetConflictingSlots(roomId, bookingForm.StartAt.Value, bookingForm.EndAt.Value);

            if (conflictedSlots.Any())
            {
                return(BadRequest(new ApiError("This time conflicts with an existing booking.")));
            }

            //TODO: Get the current user
            var userId = Guid.NewGuid();

            var bookingId = await _bookingService.CreateBookingAsync(userId, roomId, bookingForm.StartAt.Value, bookingForm.EndAt.Value);

            return(Created(Url.Link(nameof(BookingsController.GetBookingById), new { bookingId }), null));
        }
コード例 #5
0
        public async Task <IActionResult> CreateBookingForRoomAsync(
            Guid roomId,
            [FromBody] BookingForm bookingForm,
            CancellationToken cancellationToken
            )
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new ApiError(ModelState)));
            }

            var room = await _roomService.GetRoomAsync(roomId, cancellationToken);

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

            var  minimumStay = _dateLogicService.GetMinimumStay();
            bool tooShort    = (bookingForm.EndAt.Value - bookingForm.StartAt.Value) < minimumStay;

            if (tooShort)
            {
                return(BadRequest(
                           new ApiError($"The minimum booking duration is {minimumStay.TotalHours}.")
                           ));
            }

            var conflictedSlots = await _openingService.GetConflictingSlots(
                roomId, bookingForm.StartAt.Value, bookingForm.EndAt.Value, cancellationToken
                );

            if (conflictedSlots.Any())
            {
                return(BadRequest(
                           new ApiError("This time conflicts with an existing booking.")
                           ));
            }

            // Get the user ID (TODO)
            var userId = Guid.NewGuid();

            var bookingId = await _bookingService.CreateBookingAsync(
                userId, roomId, bookingForm.StartAt.Value, bookingForm.EndAt.Value, cancellationToken
                );

            return(Created(
                       Url.Link(nameof(BookingsController.GetBookingByIdAsync),
                                new { bookingId }),
                       null
                       ));
        }
コード例 #6
0
        public async Task <Guid> CreateBookingAsync(
            Guid userId,
            Guid roomId,
            DateTimeOffset startAt,
            DateTimeOffset endAt,
            CancellationToken ct)
        {
            var user = await _userManager.Users.SingleOrDefaultAsync(u => u.Id == userId, ct);

            if (user == null)
            {
                throw new InvalidOperationException("You must be logged in.");
            }

            var room = await _context.Rooms
                       .SingleOrDefaultAsync(r => r.Id == roomId, ct);

            if (room == null)
            {
                throw new ArgumentException("Invalid room id.");
            }

            var minimumStay = _dateLogicService.GetMinimumStay();
            var total       = (int)((endAt - startAt).TotalHours / minimumStay.TotalHours) * room.Rate;

            var id = Guid.NewGuid();

            var newBooknig = _context.Bookings.Add(new BookingEntity
            {
                Id         = id,
                CreatedAt  = DateTimeOffset.UtcNow,
                ModifiedAt = DateTimeOffset.UtcNow,
                StartAt    = startAt.ToUniversalTime(),
                EndAt      = endAt.ToUniversalTime(),
                Room       = room,
                User       = user,
                Total      = total
            });

            var created = await _context.SaveChangesAsync(ct);

            if (created < 1)
            {
                throw new InvalidOperationException("Could not create the booking.");
            }

            return(id);
        }
コード例 #7
0
        public static async Task AddTestData(
            HotelApiDbContext context,
            IDateLogicService dateLogicService,
            UserManager <UserEntity> userManager)
        {
            if (context.Rooms.Any())
            {
                // Already has data
                return;
            }

            context.Rooms.Add(new RoomEntity
            {
                Id   = Guid.Parse("ee2b83be-91db-4de5-8122-35a9e9195976"),
                Name = "Driscoll Suite",
                Rate = 23959
            });

            var oxford = context.Rooms.Add(new RoomEntity
            {
                Id   = Guid.Parse("301df04d-8679-4b1b-ab92-0a586ae53d08"),
                Name = "Oxford Suite",
                Rate = 10119,
            }).Entity;

            var today = DateTimeOffset.Now;
            var start = dateLogicService.AlignStartTime(today);
            var end   = start.Add(dateLogicService.GetMinimumStay());

            var adminUser = userManager.Users
                            .SingleOrDefault(u => u.Email == "*****@*****.**");

            context.Bookings.Add(new BookingEntity
            {
                Id        = Guid.Parse("2eac8dea-2749-42b3-9d21-8eb2fc0fd6bd"),
                Room      = oxford,
                CreatedAt = DateTimeOffset.UtcNow,
                StartAt   = start,
                EndAt     = end,
                Total     = oxford.Rate,
                User      = adminUser
            });

            await context.SaveChangesAsync();
        }
コード例 #8
0
        private static void AddTestData(
            HotelApiContext context,
            IDateLogicService dateLogicService,
            UserManager <UserEntity> userManager)
        {
            context.Rooms.Add(new RoomEntity
            {
                Id   = Guid.Parse("ee2b83be-91db-4de5-8122-35a9e9195976"),
                Name = "Driscoll Suite",
                Rate = 23959
            });
            // checked that data is inserted to database or not
            var insertedRoom = context.Rooms.FirstOrDefault(c => c.Id == Guid.Parse("301df04d-8679-4b1b-ab92-0a586ae53d08"));

            if (insertedRoom != null)
            {
                return;
            }
            var oxford = context.Rooms.Add(new RoomEntity
            {
                Id   = Guid.Parse("301df04d-8679-4b1b-ab92-0a586ae53d08"),
                Name = "Oxford Suite",
                Rate = 10119,
            }).Entity;

            var today     = DateTimeOffset.Now;
            var start     = dateLogicService.AlignStartTime(today);
            var end       = start.Add(dateLogicService.GetMinimumStay());
            var adminUser = userManager.Users
                            .SingleOrDefault(u => u.Email == "*****@*****.**");

            context.Bookings.Add(new BookingEntity
            {
                Id        = Guid.Parse("2eac8dea-2749-42b3-9d21-8eb2fc0fd6bd"),
                Room      = oxford,
                CreatedAt = DateTimeOffset.UtcNow,
                StartAt   = start,
                EndAt     = end,
                Total     = oxford.Rate,
                User      = adminUser
            });

            context.SaveChanges();
        }
コード例 #9
0
        public async Task <ActionResult> CreateBookingForRoom(
            Guid roomId, [FromBody] BookingForm form)
        {
            var userId = await _userService.GetUserIdAsync(User);

            if (userId == null)
            {
                return(Unauthorized());
            }

            var room = await _roomService.GetRoomAsync(roomId);

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

            var  minimumStay = _dateLogicService.GetMinimumStay();
            bool tooShort    = (form.EndAt.Value - form.StartAt.Value) < minimumStay;

            if (tooShort)
            {
                return(BadRequest(new ApiError(
                                      $"The minimum booking duration is {minimumStay.TotalHours} hours.")));
            }

            var conflictedSlots = await _openingService.GetConflictingSlots(
                roomId, form.StartAt.Value, form.EndAt.Value);

            if (conflictedSlots.Any())
            {
                return(BadRequest(new ApiError(
                                      "This time conflicts with an existing booking.")));
            }

            var bookingId = await _bookingService.CreateBookingAsync(
                userId.Value, roomId, form.StartAt.Value, form.EndAt.Value);

            return(Created(
                       Url.Link(nameof(BookingsController.GetBookingById),
                                new { bookingId }),
                       null));
        }
コード例 #10
0
        public async Task <Guid> CreateBookingAsync(
            Guid userId,
            Guid roomId,
            DateTimeOffset startAt,
            DateTimeOffset endAt,
            CancellationToken cancellationToken)
        {
            var room = await _context.Rooms
                       .SingleOrDefaultAsync(r => r.Id == roomId, cancellationToken);

            if (room == null)
            {
                throw new ArgumentException("Invalid room id.");
            }

            var minimumStay = _dateLogicService.GetMinimumStay();
            // Might be a good idea to move this logic to a class that calculates prices and rates
            var total = (int)((endAt - startAt).TotalHours / minimumStay.TotalHours) * room.Rate;

            var id = Guid.NewGuid();

            var newBooking = _context.Bookings.Add(new BookingEntity
            {
                Id         = id,
                CreatedAt  = DateTimeOffset.UtcNow,
                ModifiedAt = DateTimeOffset.UtcNow,
                StartAt    = startAt.ToUniversalTime(),
                EndAt      = endAt.ToUniversalTime(),
                Room       = room,
                Total      = total
            });

            var created = await _context.SaveChangesAsync(cancellationToken);

            if (created < 1)
            {
                throw new InvalidOperationException("Could not create the booking.");
            }

            return(id);
        }
コード例 #11
0
        public async Task <Guid> CreateBookingAsync(
            Guid userId,
            Guid roomId,
            DateTimeOffset startAt,
            DateTimeOffset endAt)
        {
            // Lookup the room reference to do it.
            var room = await _context.Rooms.SingleOrDefaultAsync(r => r.Id == roomId);

            // Error out if required.
            if (room == null)
            {
                throw new ArgumentException("Invalid room ID");
            }

            // Calculate how much
            var minimumStay = _dateLogicService.GetMinimumStay();
            var total       = (int)((endAt - startAt).TotalHours / minimumStay.TotalHours) * room.Rate;

            var bookingId = Guid.NewGuid();

            var newBooking = _context.Bookings.Add(new BookingEntity
            {
                Id         = bookingId,
                CreatedAt  = DateTimeOffset.UtcNow,
                ModifiedAt = DateTimeOffset.UtcNow,
                StartAt    = startAt.ToUniversalTime(),
                EndAt      = endAt.ToUniversalTime(),
                Total      = total,
                Room       = room
            });

            var created = await _context.SaveChangesAsync();

            if (created < 1)
            {
                throw new InvalidOperationException("Could not create the booking.");
            }

            return(bookingId);
        }
コード例 #12
0
ファイル: Startup.cs プロジェクト: mimustafa/UppsalaApi
        private static void AddTestData(
            UppsalaApiContext context,
            IDateLogicService dateLogicService,
            UserManager <UserEntity> userManager)
        {
            var roomA300 = context.Rooms.Add(new RoomEntity
            {
                Id   = Guid.Parse("301df04d-8679-4b1b-ab92-0a586ae53d08"),
                Name = "Lecture Hall A300",
                Rate = 23959
            }).Entity;

            context.Rooms.Add(new RoomEntity
            {
                Id   = Guid.Parse("ee2b83be-91db-4de5-8122-0a586ae59576"),
                Name = "Lecture Hall B200",
                Rate = 10119
            });


            var today     = DateTimeOffset.Now;
            var start     = dateLogicService.AlignStartTime(today);
            var end       = start.Add(dateLogicService.GetMinimumStay());
            var adminUser = userManager.Users
                            .SingleOrDefault(u => u.Email == "*****@*****.**");


            context.Bookings.Add(new BookingEntity
            {
                Id        = Guid.Parse("2eac8dea-2749-42b3-9d21-8eb2fc0fd6bd"),
                Room      = roomA300,
                CreatedAt = DateTimeOffset.UtcNow,
                StartAt   = start,
                EndAt     = end,
                Total     = roomA300.Rate,
                User      = adminUser
            });

            context.SaveChanges();
        }
コード例 #13
0
        public static async Task AddTestData(HotelApiDbContext context, IDateLogicService dateLogicService)
        {
            // Return if already data (probably not test instance)
            if (context.Rooms.Any())
            {
                return;
            }

            var oxford = new RoomEntity
            {
                Id   = Guid.Parse("33ed08b6-cd8c-4fe1-be31-0ac918a9a054"),
                Name = "Oxford Suite",
                Rate = 10119
            };

            context.Rooms.Add(oxford);

            context.Rooms.Add(new RoomEntity
            {
                Id   = Guid.Parse("ce9616dc-55c8-48af-b336-332733350889"),
                Name = "Driscoll Suite",
                Rate = 23959
            });

            var today = DateTimeOffset.Now;
            var start = dateLogicService.AlignStartTime(today);
            var end   = start.Add(dateLogicService.GetMinimumStay());

            context.Bookings.Add(new BookingEntity
            {
                Id        = Guid.Parse("2eac8dea-2749-42b3-9d21-8eb2fc0fd6bd"),
                Room      = oxford,
                CreatedAt = DateTimeOffset.UtcNow,
                StartAt   = start,
                EndAt     = end,
                Total     = oxford.Rate,
            });

            await context.SaveChangesAsync();
        }
コード例 #14
0
        public async Task <Guid> CreateBookingAsync(
            Guid userId,
            Guid roomId,
            DateTimeOffset startAt,
            DateTimeOffset endAt)
        {
            var room = await _context.Rooms
                       .SingleOrDefaultAsync(r => r.Id == roomId);

            if (room == null)
            {
                throw new ArgumentException("Invalid room ID.");
            }

            var minimumStay = _dateLogicService.GetMinimumStay();
            var total       = (int)((endAt - startAt).TotalHours / minimumStay.TotalHours)
                              * room.Rate;

            var id = Guid.NewGuid();

            var newBooking = await _context.Bookings.AddAsync(new BookingEntity
            {
                Id         = id,
                CreatedAt  = DateTimeOffset.UtcNow,
                ModifiedAt = DateTimeOffset.UtcNow,
                StartAt    = startAt.ToUniversalTime(),
                EndAt      = endAt.ToUniversalTime(),
                Total      = total,
                Room       = room
            });

            var created = await _context.SaveChangesAsync();

            if (created < 1)
            {
                throw new InvalidOperationException("Could not create booking.");
            }

            return(id);
        }
        public static async Task AddTestDataAsync(HotelApiDbContext context, IDateLogicService dateLogicService) //zabezpecuje seedovanie DB
        {
            if (context.Rooms.Any())
            {
                //already has data
                return;
            }

            var oxford = context.Rooms.Add(new RoomEntity
            {
                Id   = Guid.Parse("301df04d-8679-4b1b-ab92-0a586ae53d08"),
                Name = "Oxford Suite",
                Rate = 10119,
            }).Entity;

            context.Rooms.Add(new RoomEntity
            {
                Id   = Guid.Parse("ee2b83be-91db-4de5-8122-35a9e9195976"),
                Name = "Driscoll Suite",
                Rate = 23959
            });

            var today = DateTimeOffset.Now;
            var start = dateLogicService.AlignStartTime(today);
            var end   = start.Add(dateLogicService.GetMinimumStay());

            context.Bookings.Add(new BookingEntity
            {
                Id        = Guid.Parse("2eac8dea-2749-42b3-9d21-8eb2fc0fd6bd"),
                Room      = oxford,
                CreatedAt = DateTimeOffset.UtcNow,
                StartAt   = start,
                EndAt     = end,
                Total     = oxford.Rate,
            });

            await context.SaveChangesAsync();
        }
コード例 #16
0
        public async Task <ActionResult> CreateBookingForRoom(Guid roomId, [FromBody] BookingForm bookingForm)
        {
            // Assume parameters have been validated (by framework)
            var room = await _roomService.GetRoomAsync(roomId);

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

            // Not less than the allowed time for the bookings.
            var  minimumStay = _dateLogicService.GetMinimumStay();
            bool tooShort    = (bookingForm.EndAt.Value - bookingForm.StartAt.Value) < minimumStay;

            if (tooShort)
            {
                return(BadRequest(new ApiError($"Minimum booking duration is {minimumStay.TotalHours} hours")));
            }

            // Need list of conflicting slots
            var conflictingSlots = await _openingService.GetConflictingSlots(roomId, bookingForm.StartAt.Value, bookingForm.EndAt.Value);

            if (conflictingSlots.Any())
            {
                return(BadRequest(new ApiError("Time conflicts with an existing booking.")));
            }

            // TODO current user (authentication needed)
            // Need a user id, future problem to solve
            var userId = Guid.NewGuid();

            var bookingId = await _bookingService.CreateBookingAsync(userId, roomId, bookingForm.StartAt.Value, bookingForm.EndAt.Value);

            // REturn result with a link that allows navigation to the new booking
            return(Created(Url.Link(nameof(BookingsController.GetBookingById), new { bookingId }), null));
        }