-
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.
added passenger, driver and company creation commands
- Loading branch information
1 parent
81f8949
commit 614cd12
Showing
29 changed files
with
463 additions
and
29 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
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,25 @@ | ||
using BSMS.API.Extensions; | ||
using BSMS.Application.Features.Company.Commands.Create; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace BSMS.API.Controllers; | ||
|
||
/// <inheritdoc /> | ||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class CompanyController(ISender sender) : ControllerBase | ||
{ | ||
/// <summary> | ||
/// Create new transport company | ||
/// </summary> | ||
/// <param name="command">Company's data</param> | ||
/// <returns>ID of the created company</returns> | ||
[HttpPost("Create")] | ||
public async Task<ActionResult<int>> Create(CreateCompanyCommand command) | ||
{ | ||
var result = await sender.Send(command); | ||
|
||
return result.DecideWhatToReturn(); | ||
} | ||
} |
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,25 @@ | ||
using BSMS.API.Extensions; | ||
using BSMS.Application.Features.Driver.Commands.Create; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace BSMS.API.Controllers; | ||
|
||
/// <inheritdoc /> | ||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class DriverController(ISender mediator) : ControllerBase | ||
{ | ||
/// <summary> | ||
/// Create new driver (with company and bus (both optional)) | ||
/// </summary> | ||
/// <param name="command">Driver data</param> | ||
/// <returns>ID of the created driver</returns> | ||
[HttpPost("Create")] | ||
public async Task<ActionResult<int>> Create(CreateDriverCommand command) | ||
{ | ||
var result = await mediator.Send(command); | ||
|
||
return result.DecideWhatToReturn(); | ||
} | ||
} |
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,25 @@ | ||
using BSMS.API.Extensions; | ||
using BSMS.Application.Features.Passenger.Commands.Create; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace BSMS.API.Controllers; | ||
|
||
/// <inheritdoc /> | ||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class PassengerController(ISender mediator) : ControllerBase | ||
{ | ||
/// <summary> | ||
/// Create new passenger | ||
/// </summary> | ||
/// <param name="command">Passenger's data</param> | ||
/// <returns>ID of the created passenger</returns> | ||
[HttpPost("Create")] | ||
public async Task<ActionResult<int>> Create(CreatePassengerCommand command) | ||
{ | ||
var result = await mediator.Send(command); | ||
|
||
return result.DecideWhatToReturn(); | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
src/BSMS.Application/Contracts/Persistence/ICompanyRepository.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,7 @@ | ||
using BSMS.Core.Entities; | ||
|
||
namespace BSMS.Application.Contracts.Persistence; | ||
|
||
public interface ICompanyRepository : IGenericRepository<Company> | ||
{ | ||
} |
7 changes: 7 additions & 0 deletions
7
src/BSMS.Application/Contracts/Persistence/IDriverRepository.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,7 @@ | ||
using BSMS.Core.Entities; | ||
|
||
namespace BSMS.Application.Contracts.Persistence; | ||
|
||
public interface IDriverRepository : IGenericRepository<Driver> | ||
{ | ||
} |
7 changes: 7 additions & 0 deletions
7
src/BSMS.Application/Contracts/Persistence/IPassengerRepository.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,7 @@ | ||
using BSMS.Core.Entities; | ||
|
||
namespace BSMS.Application.Contracts.Persistence; | ||
|
||
public interface IPassengerRepository : IGenericRepository<Passenger> | ||
{ | ||
} |
8 changes: 8 additions & 0 deletions
8
src/BSMS.Application/Contracts/Persistence/IRouteRepository.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,8 @@ | ||
using BSMS.Core.Entities; | ||
|
||
namespace BSMS.Application.Contracts.Persistence; | ||
|
||
public interface IRouteRepository : IGenericRepository<Route> | ||
{ | ||
|
||
} |
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
44 changes: 44 additions & 0 deletions
44
src/BSMS.Application/Features/Company/Commands/Create/CreateCompanyCommandHandler.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,44 @@ | ||
using System.Net; | ||
using BSMS.Application.Contracts.Persistence; | ||
using BSMS.Application.Extensions; | ||
using BSMS.Application.Features.Common; | ||
using BSMS.Application.Helpers; | ||
using FluentValidation; | ||
using MapsterMapper; | ||
using MediatR; | ||
|
||
namespace BSMS.Application.Features.Company.Commands.Create; | ||
|
||
public record CreateCompanyCommand( | ||
string Name, | ||
string Address, | ||
string Phone, | ||
string Email) : IRequest<MethodResult<CreatedEntityResponse>>; | ||
|
||
public class CreateCompanyCommandHandler( | ||
ICompanyRepository repository, | ||
IMapper mapper, | ||
IValidator<CreateCompanyCommand> validator, | ||
MethodResultFactory methodResultFactory) | ||
: IRequestHandler<CreateCompanyCommand, MethodResult<CreatedEntityResponse>> | ||
{ | ||
public async Task<MethodResult<CreatedEntityResponse>> Handle(CreateCompanyCommand request, CancellationToken cancellationToken) | ||
{ | ||
var result = methodResultFactory.Create<CreatedEntityResponse>(); | ||
|
||
var validationResult = await validator.ValidateAsync(request, cancellationToken); | ||
if (!validationResult.IsValid) | ||
{ | ||
result.SetError(validationResult.Errors.ToResponse(), HttpStatusCode.BadRequest); | ||
return result; | ||
} | ||
|
||
var company = mapper.Map<Core.Entities.Company>(request); | ||
|
||
await repository.InsertAsync(company); | ||
|
||
result.Data = new CreatedEntityResponse(company.CompanyId); | ||
|
||
return result; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/BSMS.Application/Features/Company/Commands/Create/CreateCompanyCommandValidator.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,28 @@ | ||
using BSMS.Application.Helpers; | ||
using FluentValidation; | ||
|
||
namespace BSMS.Application.Features.Company.Commands.Create; | ||
|
||
public class CreateCompanyCommandValidator : AbstractValidator<CreateCompanyCommand> | ||
{ | ||
public CreateCompanyCommandValidator() | ||
{ | ||
RuleFor(c => c.Name) | ||
.NotEmpty() | ||
.NotNull() | ||
.Length(3, 50) | ||
.Matches(RegexConstants.LettersOnly) | ||
.WithMessage("{PropertyName} must consist only from letters"); | ||
|
||
RuleFor(c => c.Phone) | ||
.NotEmpty() | ||
.Matches(RegexConstants.PhoneNumber) | ||
.WithMessage("Invalid phone number format"); | ||
|
||
RuleFor(c => c.Email) | ||
.NotEmpty() | ||
.MaximumLength(50) | ||
.Matches(RegexConstants.Email) | ||
.WithMessage("Invalid email format"); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/BSMS.Application/Features/Driver/Commands/Create/CreateDriverCommandHandler.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,44 @@ | ||
using System.Net; | ||
using BSMS.Application.Contracts.Persistence; | ||
using BSMS.Application.Extensions; | ||
using BSMS.Application.Features.Common; | ||
using BSMS.Application.Helpers; | ||
using FluentValidation; | ||
using MapsterMapper; | ||
using MediatR; | ||
|
||
namespace BSMS.Application.Features.Driver.Commands.Create; | ||
|
||
public record CreateDriverCommand( | ||
string FirstName, | ||
string LastName, | ||
string DriverLicense, | ||
int? CompanyId, | ||
int? BusId) : IRequest<MethodResult<CreatedEntityResponse>>; | ||
|
||
public class CreateDriverCommandHandler( | ||
IDriverRepository repository, | ||
IMapper mapper, | ||
IValidator<CreateDriverCommand> validator, | ||
MethodResultFactory methodResultFactory) : IRequestHandler<CreateDriverCommand, MethodResult<CreatedEntityResponse>> | ||
{ | ||
public async Task<MethodResult<CreatedEntityResponse>> Handle(CreateDriverCommand request, CancellationToken cancellationToken) | ||
{ | ||
var result = methodResultFactory.Create<CreatedEntityResponse>(); | ||
|
||
var validationResult = await validator.ValidateAsync(request, cancellationToken); | ||
if (!validationResult.IsValid) | ||
{ | ||
result.SetError(validationResult.Errors.ToResponse(), HttpStatusCode.BadRequest); | ||
return result; | ||
} | ||
|
||
var driver = mapper.Map<Core.Entities.Driver>(request); | ||
|
||
await repository.InsertAsync(driver); | ||
|
||
result.Data = new CreatedEntityResponse(driver.DriverId); | ||
|
||
return result; | ||
} | ||
} |
Oops, something went wrong.