This repository has been archived by the owner on Nov 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Добавить команду создания брони комнаты
- Loading branch information
Showing
11 changed files
with
155 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
|
||
namespace Common.Extensions | ||
{ | ||
public static class ExpressionExtension | ||
{ | ||
public static bool Between(this DateTime @this, DateTime left, DateTime right) | ||
{ | ||
return left <= @this && @this <= right; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/Application/CQS/Reservation/Command/CreateReservationCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Application.CQS.Reservation.Exception; | ||
using Common.Extensions; | ||
using Domain; | ||
using Domain.Entities; | ||
using JetBrains.Annotations; | ||
using NHibernate.Linq; | ||
|
||
namespace Application.CQS.Reservation.Command | ||
{ | ||
public class CreateReservationCommand : IUserAware | ||
{ | ||
public UserEntity? CurrentUser { get; set; } | ||
private IRepository<ReservationEntity> ReservationRepository { get; } | ||
private IRepository<RoomEntity> RoomRepository { get; } | ||
private IRepository<ServiceEntity> ServiceRepository { get; } | ||
|
||
public CreateReservationCommand( | ||
IRepository<ReservationEntity> reservationRepository, | ||
IRepository<RoomEntity> roomRepository, | ||
IRepository<ServiceEntity> serviceRepository | ||
) | ||
{ | ||
ReservationRepository = reservationRepository; | ||
RoomRepository = roomRepository; | ||
ServiceRepository = serviceRepository; | ||
} | ||
|
||
public async Task ExecuteAsync(ReservationInput input) | ||
{ | ||
AssertRentDatesValid(input); | ||
await AssertRentDatesNotBusyAsync(input); | ||
|
||
var services = await ServiceRepository.FindAll() | ||
.Where(service => input.ServiceIds.Contains(service.Id)) | ||
.ToListAsync(); | ||
|
||
var room = await RoomRepository.GetAsync(input.RoomId); | ||
|
||
var reservation = new ReservationEntity(CurrentUser!, room, input.ReservedFrom, input.ReservedTo, services); | ||
|
||
await ReservationRepository.SaveAndFlushAsync(reservation); | ||
} | ||
|
||
[AssertionMethod] | ||
private void AssertRentDatesValid(ReservationInput input) | ||
{ | ||
if (DateTime.Now > input.ReservedTo) | ||
{ | ||
throw CreateReservationException.DateToCantBeInPast(); | ||
} | ||
|
||
if (input.ReservedFrom <= input.ReservedTo) | ||
{ | ||
throw CreateReservationException.InvalidDateValues(); | ||
} | ||
} | ||
|
||
[AssertionMethod] | ||
private async Task AssertRentDatesNotBusyAsync(ReservationInput input) | ||
{ | ||
var reservations = ReservationRepository | ||
.FindAll() | ||
.Where(r => input.RoomId == r.Room.Id | ||
&& (input.ReservedFrom.Between(r.ReservedFrom, r.ReservedTo) | ||
|| input.ReservedTo.Between(r.ReservedFrom, r.ReservedTo))); | ||
|
||
if (0 != await reservations.CountAsync()) | ||
{ | ||
throw CreateReservationException.DatesAreBusy(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/Application/CQS/Reservation/Exception/CreateReservationException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace Application.CQS.Reservation.Exception | ||
{ | ||
public class CreateReservationException : System.Exception | ||
{ | ||
public CreateReservationException(string message): base(message) | ||
{ | ||
} | ||
|
||
public static CreateReservationException DateToCantBeInPast() | ||
{ | ||
return new CreateReservationException("Reservatoin date 'From' can't be in past."); | ||
} | ||
|
||
public static CreateReservationException InvalidDateValues() | ||
{ | ||
return new CreateReservationException("Reservation date 'From' must me smaller than date 'To'."); | ||
} | ||
|
||
public static CreateReservationException DatesAreBusy() | ||
{ | ||
return new CreateReservationException("Specified rent dates are busy."); | ||
} | ||
} | ||
} |
4 changes: 1 addition & 3 deletions
4
...n/CQS/Reservation/ReservationException.cs → ...n/Exception/DeleteReservationException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Application.CQS.Reservation | ||
{ | ||
public class ReservationInput | ||
{ | ||
public Guid RoomId { get; set; } | ||
public DateTime ReservedFrom { get; set; } | ||
public DateTime ReservedTo { get; set; } | ||
public IEnumerable<Guid> ServiceIds { get; set; } | ||
|
||
public ReservationInput(Guid roomId, DateTime reservedFrom, DateTime reservedTo, IEnumerable<Guid> serviceIds) | ||
{ | ||
RoomId = roomId; | ||
ReservedFrom = reservedFrom; | ||
ReservedTo = reservedTo; | ||
ServiceIds = serviceIds; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters