Skip to content

Commit

Permalink
feat: Adiciona endpoint de Starship
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisACorrea committed Apr 28, 2024
1 parent 841603c commit 98b8fc5
Show file tree
Hide file tree
Showing 13 changed files with 285 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/MayTheFourth/MayTheFourth.API/Helpers/Urls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@ public static class Urls
public static string PostVehicle => "/Vehicles";
public static string PutVehicleById => "/Vehicles/{Id}";
public static string DeleteVehicleById => "/Vehicles/{Id}";

public static string GetStarshipsList => "/Starships/All";
public static string GetStarshipById => "/Starships/{Id}";
public static string PostStarship => "/Starships";
public static string PutStarshipById => "/Starships/{Id}";
public static string DeleteStarshipById => "/Starships/{Id}";
}
}
1 change: 1 addition & 0 deletions src/MayTheFourth/MayTheFourth.API/Routes/Routes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public static void MapRoutes(this IEndpointRouteBuilder app)
app.MapCharactersRoutes();
app.MapMoviesRoutes();
app.MapVehiclesRoutes();
app.MapStarshipsRoutes();
}
}
88 changes: 88 additions & 0 deletions src/MayTheFourth/MayTheFourth.API/Routes/StarshipRoutes.cs
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");
}
}
4 changes: 4 additions & 0 deletions src/MayTheFourth/MayTheFourth.IoC/IocServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ public static void AddServices(this IServiceCollection services, IConfiguration
services.AddAutoMapper(typeof(ViewToModelCharacterProfile));
services.AddAutoMapper(typeof(ModelToViewVehicleProfile));
services.AddAutoMapper(typeof(ViewToModelVehicleProfile));
services.AddAutoMapper(typeof(ModelToViewStarshipProfile));
services.AddAutoMapper(typeof(ViewToModelStarshipProfile));

services.AddScoped<IMovieRepository, MovieRepository>();
services.AddScoped<ICharacterRepository, CharacterRepository>();
services.AddScoped<IVehicleRepository, VehicleRepository>();
services.AddScoped<IStarshipRepository, StarshipRepository>();

services.AddScoped<IMovieService, MovieService>();
services.AddScoped<ICharacterService, CharacterService>();
services.AddScoped<IVehicleService, VehicleService>();
services.AddScoped<IStarshipService, StarshipService>();

}
}
Expand Down
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>
{
}
}
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
{
}
}
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
{
}
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>>();
}
}
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>>();
}
}
12 changes: 12 additions & 0 deletions src/MayTheFourth/MayTheFourth.Services/StarshipService.cs
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 src/MayTheFourth/MayTheFourth.Services/ViewModels/StarshipVM.cs
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);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/MayTheFourth/MayTheFourth.Utils/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@
<data name="HeightIsRequired" xml:space="preserve">
<value>Altura é obrigatória</value>
</data>
<data name="HyperdriveRatingIsRequired" xml:space="preserve">
<value>Classificação do Hyperdrive é obrigatório</value>
</data>
<data name="InvalidConversion" xml:space="preserve">
<value>Conversão inválida</value>
</data>
Expand All @@ -168,6 +171,9 @@
<data name="MaxSpeedIsRequired" xml:space="preserve">
<value>Velocidade máxima é obrigatório</value>
</data>
<data name="MGLTIsRequired" xml:space="preserve">
<value>MGLT é obrigatório</value>
</data>
<data name="ModelIsRequired" xml:space="preserve">
<value>Modelo é obrigatório</value>
</data>
Expand Down

0 comments on commit 98b8fc5

Please sign in to comment.