forked from balta-io/desafio-balta-may-the-fourth-backend
-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
841603c
commit 98b8fc5
Showing
13 changed files
with
285 additions
and
2 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
88 changes: 88 additions & 0 deletions
88
src/MayTheFourth/MayTheFourth.API/Routes/StarshipRoutes.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,88 @@ | ||
using MayTheFourth.API.Helpers; | ||
using MayTheFourth.Entities; | ||
using MayTheFourth.Services.Interfaces; | ||
using MayTheFourth.Services.ViewModels; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace MayTheFourth.API.Routes; | ||
|
||
public static class StarshipRoutes | ||
{ | ||
public static void MapStarshipsRoutes(this IEndpointRouteBuilder app) | ||
{ | ||
app.MapGet(Urls.GetStarshipsList, async ( | ||
[FromQuery(Name = "page")] int? page, | ||
[FromQuery(Name = "limit")] int? limit, | ||
IStarshipService service, CancellationToken cancellation) => | ||
{ | ||
return await ApiHelper.GetAllPagedAsync( | ||
service, | ||
page ?? 0, | ||
limit ?? 0, | ||
cancellation); | ||
}) | ||
.WithName(nameof(Urls.GetStarshipsList)) | ||
.WithOpenApi() | ||
.WithTags("Starships"); | ||
|
||
app.MapGet(Urls.GetStarshipById, async ( | ||
IStarshipService service, | ||
Guid id, | ||
CancellationToken cancellation) => | ||
{ | ||
return await ApiHelper.GetByKeyAsync( | ||
service, r => r.Id == id, | ||
cancellation); | ||
}) | ||
.WithName(nameof(Urls.GetStarshipById)) | ||
.WithOpenApi() | ||
.WithTags("Starships"); | ||
|
||
|
||
app.MapPost(Urls.PostStarship, async ( | ||
IStarshipService service, | ||
StarshipVM starship, | ||
CancellationToken cancellation) => | ||
{ | ||
var result = await service.CreateAsync(starship, cancellation); | ||
|
||
return ApiHelper.ResultOperation<StarshipVM, Starship>( | ||
result, service | ||
); | ||
}) | ||
.WithName(nameof(Urls.PostStarship)) | ||
.WithOpenApi() | ||
.WithTags("Starships"); | ||
|
||
app.MapPut(Urls.PutStarshipById, async ( | ||
IStarshipService service, | ||
Guid id, | ||
StarshipVM starship, | ||
CancellationToken cancellation) => | ||
{ | ||
starship.Id = id; | ||
|
||
var result = await service.ChangeAsync(starship, cancellation); | ||
|
||
return ApiHelper.ResultOperation<StarshipVM, Starship>( | ||
result, service | ||
); | ||
}) | ||
.WithName(nameof(Urls.PutStarshipById)) | ||
.WithOpenApi() | ||
.WithTags("Starships"); | ||
|
||
app.MapDelete(Urls.DeleteStarshipById, async ( | ||
IStarshipService service, | ||
Guid id, | ||
CancellationToken cancellation) => | ||
{ | ||
return await ApiHelper.RemoveByIdAsync( | ||
service, id, | ||
cancellation); | ||
}) | ||
.WithName(nameof(Urls.DeleteStarshipById)) | ||
.WithOpenApi() | ||
.WithTags("Starships"); | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
src/MayTheFourth/MayTheFourth.Repositories/Repositories/Interfaces/IStarshipRepository.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,10 @@ | ||
using MayTheFourth.Entities; | ||
|
||
namespace MayTheFourth.Repositories.Repositories.Interfaces | ||
{ | ||
public interface IStarshipRepository : | ||
IBaseReaderRepository<Starship>, | ||
IBaseWriterRepository<Starship> | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/MayTheFourth/MayTheFourth.Repositories/Repositories/StarshipRepository.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,12 @@ | ||
using MayTheFourth.Entities; | ||
using MayTheFourth.Repositories.Context; | ||
using MayTheFourth.Repositories.Repositories.Interfaces; | ||
|
||
namespace MayTheFourth.Repositories.Repositories | ||
{ | ||
public class StarshipRepository(DatabaseContext context) : | ||
BaseRepository<Starship>(context), | ||
IStarshipRepository | ||
{ | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/MayTheFourth/MayTheFourth.Services/Interfaces/IStarshipService.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,10 @@ | ||
using MayTheFourth.Entities; | ||
using MayTheFourth.Services.ViewModels; | ||
|
||
namespace MayTheFourth.Services.Interfaces; | ||
public interface IStarshipService : | ||
IBaseReaderService<StarshipVM, Starship>, | ||
IBaseWriterService<StarshipVM, Starship>, | ||
IErrorBaseService | ||
{ | ||
} |
15 changes: 15 additions & 0 deletions
15
src/MayTheFourth/MayTheFourth.Services/Mappers/Profiles/ModelToViewStarshipProfile.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,15 @@ | ||
using AutoMapper; | ||
using MayTheFourth.Entities; | ||
using MayTheFourth.Services.ViewModels; | ||
using MayTheFourth.Utils.Paging; | ||
|
||
namespace MayTheFourth.Services.Mappers.Profiles; | ||
|
||
public class ModelToViewStarshipProfile : Profile | ||
{ | ||
public ModelToViewStarshipProfile() | ||
{ | ||
CreateMap<Starship, StarshipVM>(); | ||
CreateMap<PageListResult<Starship>, PageListResult<StarshipVM>>(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/MayTheFourth/MayTheFourth.Services/Mappers/Profiles/ViewToModelStarshipProfile.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,15 @@ | ||
using AutoMapper; | ||
using MayTheFourth.Entities; | ||
using MayTheFourth.Services.ViewModels; | ||
using MayTheFourth.Utils.Paging; | ||
|
||
namespace MayTheFourth.Services.Mappers.Profiles; | ||
|
||
public class ViewToModelStarshipProfile : Profile | ||
{ | ||
public ViewToModelStarshipProfile() | ||
{ | ||
CreateMap<StarshipVM, Starship>(); | ||
CreateMap<PageListResult<StarshipVM>, PageListResult<Starship>>(); | ||
} | ||
} |
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 MayTheFourth.Entities; | ||
using MayTheFourth.Repositories.Repositories.Interfaces; | ||
using MayTheFourth.Services.Interfaces; | ||
using MayTheFourth.Services.ViewModels; | ||
|
||
namespace MayTheFourth.Services; | ||
|
||
public class StarshipService(IStarshipRepository repository) : | ||
BaseService<StarshipVM, Starship>(repository, repository), | ||
IStarshipService | ||
{ | ||
} |
90 changes: 88 additions & 2 deletions
90
src/MayTheFourth/MayTheFourth.Services/ViewModels/StarshipVM.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 |
---|---|---|
@@ -1,8 +1,94 @@ | ||
using MayTheFourth.Entities; | ||
using MayTheFourth.Utils.Validation; | ||
|
||
namespace MayTheFourth.Services.ViewModels | ||
namespace MayTheFourth.Services.ViewModels; | ||
|
||
public class StarshipVM : BaseViewModel<Starship> | ||
{ | ||
public class StarshipVM : BaseViewModel<Starship> | ||
public StarshipVM() | ||
{ | ||
Movies = []; | ||
} | ||
|
||
public string Name { get; set; } = string.Empty; | ||
public string Model { get; set; } = string.Empty; | ||
public string Manufacturer { get; set; } = string.Empty; | ||
public string CostInCredits { get; set; } = string.Empty; | ||
public string Length { get; set; } = string.Empty; | ||
public string MaxSpeed { get; set; } = string.Empty; | ||
public string Crew { get; set; } = string.Empty; | ||
public string Passengers { get; set; } = string.Empty; | ||
public string CargoCapacity { get; set; } = string.Empty; | ||
public string HyperdriveRating { get; set; } = string.Empty; | ||
public string MGLT { get; set; } = string.Empty; | ||
public string Consumables { get; set; } = string.Empty; | ||
public string Class { get; set; } = string.Empty; | ||
public ICollection<Movie> Movies { get; set; } | ||
|
||
public override Starship GetEntity() => | ||
new() | ||
{ | ||
Id = Id, | ||
Name = Name, | ||
Model = Model, | ||
Manufacturer = Manufacturer, | ||
CostInCredits = CostInCredits, | ||
Length = Length, | ||
MaxSpeed = MaxSpeed, | ||
Crew = Crew, | ||
Passengers = Passengers, | ||
CargoCapacity = CargoCapacity, | ||
HyperdriveRating = HyperdriveRating, | ||
MGLT = MGLT, | ||
Consumables = Consumables, | ||
Class = Class, | ||
Movies = Movies | ||
}; | ||
|
||
public override ValidationViewModel IsValid() | ||
{ | ||
var resultMessages = new List<string>(); | ||
if (string.IsNullOrEmpty(Name)) | ||
resultMessages.Add(Utils.Properties.Resources.NameIsRequired); | ||
|
||
if (string.IsNullOrEmpty(Model)) | ||
resultMessages.Add(Utils.Properties.Resources.ModelIsRequired); | ||
|
||
if (string.IsNullOrEmpty(Manufacturer)) | ||
resultMessages.Add(Utils.Properties.Resources.ManufacturerIsRequired); | ||
|
||
if (string.IsNullOrEmpty(CostInCredits)) | ||
resultMessages.Add(Utils.Properties.Resources.CostInCreditsIsRequired); | ||
|
||
if (string.IsNullOrEmpty(Length)) | ||
resultMessages.Add(Utils.Properties.Resources.LengthIsRequired); | ||
|
||
if (string.IsNullOrEmpty(MaxSpeed)) | ||
resultMessages.Add(Utils.Properties.Resources.MaxSpeedIsRequired); | ||
|
||
if (string.IsNullOrEmpty(Crew)) | ||
resultMessages.Add(Utils.Properties.Resources.CrewIsRequired); | ||
|
||
if (string.IsNullOrEmpty(Passengers)) | ||
resultMessages.Add(Utils.Properties.Resources.PassengersIsRequired); | ||
|
||
if (string.IsNullOrEmpty(CargoCapacity)) | ||
resultMessages.Add(Utils.Properties.Resources.CargoCapacityIsRequired); | ||
|
||
if (string.IsNullOrEmpty(HyperdriveRating)) | ||
resultMessages.Add(Utils.Properties.Resources.HyperdriveRatingIsRequired); | ||
|
||
if (string.IsNullOrEmpty(MGLT)) | ||
resultMessages.Add(Utils.Properties.Resources.MGLTIsRequired); | ||
|
||
if (string.IsNullOrEmpty(Consumables)) | ||
resultMessages.Add(Utils.Properties.Resources.ConsumablesIsRequired); | ||
|
||
if (string.IsNullOrEmpty(Class)) | ||
resultMessages.Add(Utils.Properties.Resources.ClassIsRequired); | ||
|
||
return resultMessages.Any() ? | ||
ValidationViewModel.Create(ValidationModelResult.Warning, resultMessages.ToArray()) : | ||
ValidationViewModel.Create(ValidationModelResult.Success); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/MayTheFourth/MayTheFourth.Utils/Properties/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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