Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjusts in swagger #3

Merged
merged 5 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/MayTheFourth/MayTheFourth.API/Helpers/ApiHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static IResult ResultErrorOperation(
{
return Results.BadRequest(new
{
Error = IsSuccessResponse(statusCode),
Error = !IsSuccessResponse(statusCode),
Status = (int)statusCode,
Detail = new string[] {
$"{ex.Message} {ex.InnerException?.Message} {ex.InnerException?.InnerException?.Message}"
Expand All @@ -73,14 +73,14 @@ public static IResult ResultPagedOperation<ViewModel, Model>(
if (service.Validation.Any())
return Results.BadRequest(new
{
Error = IsSuccessResponse(HttpStatusCode.BadRequest),
Error = !IsSuccessResponse(HttpStatusCode.BadRequest),
Status = (int)HttpStatusCode.BadRequest,
Detail = service.Validation.Select(x => string.Join(" ", x.Messages)).ToArray()
});

return Results.Ok(new
{
Error = IsSuccessResponse(HttpStatusCode.OK),
Error = !IsSuccessResponse(HttpStatusCode.OK),
Status = (int)HttpStatusCode.OK,
Data = OkResult
});
Expand All @@ -95,14 +95,14 @@ public static IResult ResultOperation<ViewModel, Model>(
if (service.Validation.Any() || OkResult == null)
return Results.BadRequest(new
{
Error = IsSuccessResponse(HttpStatusCode.BadRequest),
Error = !IsSuccessResponse(HttpStatusCode.BadRequest),
Status = (int)HttpStatusCode.BadRequest,
Detail = service.Validation.Select(x => string.Join(" ", x.Messages)).ToArray()
});

return Results.Ok(new
{
Error = IsSuccessResponse(HttpStatusCode.OK),
Error = !IsSuccessResponse(HttpStatusCode.OK),
Status = (int)HttpStatusCode.OK,
Data = OkResult
});
Expand All @@ -117,14 +117,14 @@ public static IResult ResultListOperation<ViewModel, Model>(
if (service.Validation.Any())
return Results.BadRequest(new
{
Error = IsSuccessResponse(HttpStatusCode.BadRequest),
Error = !IsSuccessResponse(HttpStatusCode.BadRequest),
Status = (int)HttpStatusCode.BadRequest,
Detail = service.Validation.Select(x => string.Join(" ", x.Messages)).ToArray()
});

return Results.Ok(new
{
Error = IsSuccessResponse(HttpStatusCode.OK),
Error = !IsSuccessResponse(HttpStatusCode.OK),
Status = (int)HttpStatusCode.OK,
Data = OkResult
});
Expand Down
5 changes: 5 additions & 0 deletions src/MayTheFourth/MayTheFourth.API/Helpers/Urls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ public static class Urls
public static string PostMovie => "/Movies";
public static string PutMovieById => "/Movies/{Id}";
public static string DeleteMovieById => "/Movies/{Id}";
public static string GetCharactersList => "/Characters/All";
public static string GetCharacterById => "/Characters/{Id}";
public static string PostCharacter => "/Characters";
public static string PutCharacterById => "/Characters/{Id}";
public static string DeleteCharacterById => "/Characters/{Id}";
}
}
92 changes: 13 additions & 79 deletions src/MayTheFourth/MayTheFourth.API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,110 +1,44 @@
using AutoMapper;
using MayTheFourth.API.Helpers;
using MayTheFourth.API.Routes;
using MayTheFourth.Entities;
using MayTheFourth.IoC;
using MayTheFourth.Services.Interfaces;
using MayTheFourth.Services.Mappers;
using MayTheFourth.Services.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Models;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "MayTheFourth", Version = "v1" });
});

builder.Services.AddServices(builder.Configuration);


builder.Services.ConfigureHttpJsonOptions(options => {
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.WriteIndented = true;
options.SerializerOptions.IncludeFields = true;
});

var app = builder.Build();

app.UseSwagger();
app.UseSwaggerUI();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("./v1/swagger.json", "MayTheFourth");
});

MapperModel.SetMapper(app.Services.GetRequiredService<IMapper>());

app.UseHttpsRedirection();

app.MapGet(Urls.GetMoviesList, async (
[FromQuery(Name = "page")] int? page,
[FromQuery(Name = "limit")] int? limit,
IMovieService service, CancellationToken cancellation) =>
{
return await ApiHelper.GetAllPagedAsync(
service,
page ?? 0,
limit ?? 0,
cancellation);
})
.WithName(nameof(Urls.GetMoviesList))
.WithOpenApi()
.WithTags("Movies");

app.MapGet(Urls.GetMovieById, async (
IMovieService service,
Guid id,
CancellationToken cancellation) =>
{
return await ApiHelper.GetByKeyAsync(
service, r => r.Id == id,
cancellation);
})
.WithName(nameof(Urls.GetMovieById))
.WithOpenApi()
.WithTags("Movies");


app.MapPost(Urls.PostMovie, async (
IMovieService service,
MovieVM movie,
CancellationToken cancellation) =>
{
var result = await service.CreateAsync(movie, cancellation);

return ApiHelper.ResultOperation<MovieVM, Movie>(
result, service
);
})
.WithName(nameof(Urls.PostMovie))
.WithOpenApi()
.WithTags("Movies");

app.MapPut(Urls.PutMovieById, async (
IMovieService service,
Guid id,
MovieVM movie,
CancellationToken cancellation) =>
{
movie.Id = id;

var result = await service.ChangeAsync(movie, cancellation);

return ApiHelper.ResultOperation<MovieVM, Movie>(
result, service
);
})
.WithName(nameof(Urls.PutMovieById))
.WithOpenApi()
.WithTags("Movies");

app.MapDelete(Urls.DeleteMovieById, async (
IMovieService service,
Guid id,
CancellationToken cancellation) =>
{
return await ApiHelper.RemoveByIdAsync(
service, id,
cancellation);
})
.WithName(nameof(Urls.DeleteMovieById))
.WithOpenApi()
.WithTags("Movies");
app.MapRoutes();

app.Run();

88 changes: 88 additions & 0 deletions src/MayTheFourth/MayTheFourth.API/Routes/CharacterRoutes.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 CharacterRoutes
{
public static void MapCharactersRoutes(this IEndpointRouteBuilder app)
{
app.MapGet(Urls.GetCharactersList, async (
[FromQuery(Name = "page")] int? page,
[FromQuery(Name = "limit")] int? limit,
ICharacterService service, CancellationToken cancellation) =>
{
return await ApiHelper.GetAllPagedAsync(
service,
page ?? 0,
limit ?? 0,
cancellation);
})
.WithName(nameof(Urls.GetCharactersList))
.WithOpenApi()
.WithTags("Characters");

app.MapGet(Urls.GetCharacterById, async (
ICharacterService service,
Guid id,
CancellationToken cancellation) =>
{
return await ApiHelper.GetByKeyAsync(
service, r => r.Id == id,
cancellation);
})
.WithName(nameof(Urls.GetCharacterById))
.WithOpenApi()
.WithTags("Characters");


app.MapPost(Urls.PostCharacter, async (
ICharacterService service,
CharacterVM movie,
CancellationToken cancellation) =>
{
var result = await service.CreateAsync(movie, cancellation);

return ApiHelper.ResultOperation<CharacterVM, Character>(
result, service
);
})
.WithName(nameof(Urls.PostCharacter))
.WithOpenApi()
.WithTags("Characters");

app.MapPut(Urls.PutCharacterById, async (
ICharacterService service,
Guid id,
CharacterVM movie,
CancellationToken cancellation) =>
{
movie.Id = id;

var result = await service.ChangeAsync(movie, cancellation);

return ApiHelper.ResultOperation<CharacterVM, Character>(
result, service
);
})
.WithName(nameof(Urls.PutCharacterById))
.WithOpenApi()
.WithTags("Characters");

app.MapDelete(Urls.DeleteCharacterById, async (
ICharacterService service,
Guid id,
CancellationToken cancellation) =>
{
return await ApiHelper.RemoveByIdAsync(
service, id,
cancellation);
})
.WithName(nameof(Urls.DeleteCharacterById))
.WithOpenApi()
.WithTags("Characters");
}
}
89 changes: 89 additions & 0 deletions src/MayTheFourth/MayTheFourth.API/Routes/MoviesRoutes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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 MoviesRoutes
{
public static void MapMoviesRoutes(this IEndpointRouteBuilder app)
{
app.MapGet(Urls.GetMoviesList, async (
[FromQuery(Name = "page")] int? page,
[FromQuery(Name = "limit")] int? limit,
IMovieService service, CancellationToken cancellation) =>
{
return await ApiHelper.GetAllPagedAsync(
service,
page ?? 0,
limit ?? 0,
cancellation);
})
.WithName(nameof(Urls.GetMoviesList))
.WithOpenApi()
.WithTags("Movies");

app.MapGet(Urls.GetMovieById, async (
IMovieService service,
Guid id,
CancellationToken cancellation) =>
{
return await ApiHelper.GetByKeyAsync(
service, r => r.Id == id,
cancellation);
})
.WithName(nameof(Urls.GetMovieById))
.WithOpenApi()
.WithTags("Movies");


app.MapPost(Urls.PostMovie, async (
IMovieService service,
MovieVM movie,
CancellationToken cancellation) =>
{
var result = await service.CreateAsync(movie, cancellation);

return ApiHelper.ResultOperation<MovieVM, Movie>(
result, service
);
})
.WithName(nameof(Urls.PostMovie))
.WithOpenApi()
.WithTags("Movies");

app.MapPut(Urls.PutMovieById, async (
IMovieService service,
Guid id,
MovieVM movie,
CancellationToken cancellation) =>
{
movie.Id = id;

var result = await service.ChangeAsync(movie, cancellation);

return ApiHelper.ResultOperation<MovieVM, Movie>(
result, service
);
})
.WithName(nameof(Urls.PutMovieById))
.WithOpenApi()
.WithTags("Movies");

app.MapDelete(Urls.DeleteMovieById, async (
IMovieService service,
Guid id,
CancellationToken cancellation) =>
{
return await ApiHelper.RemoveByIdAsync(
service, id,
cancellation);
})
.WithName(nameof(Urls.DeleteMovieById))
.WithOpenApi()
.WithTags("Movies");
}
}
}
18 changes: 18 additions & 0 deletions src/MayTheFourth/MayTheFourth.API/Routes/Routes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.AspNetCore.Http.HttpResults;

namespace MayTheFourth.API.Routes;

public static class Routes
{
public static void MapRoutes(this IEndpointRouteBuilder app)
{
app.MapGet("/", context =>
{
context.Response.Redirect("/swagger/");
return Task.CompletedTask;
});

app.MapCharactersRoutes();
app.MapMoviesRoutes();
}
}
Loading
Loading