Add Refit SDK

This commit is contained in:
Alex Hyett 2023-07-07 11:54:49 +01:00
parent d4fc49c8d1
commit 0cb97f48df
25 changed files with 121 additions and 123 deletions

View file

@ -1,9 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View file

@ -1,4 +1,4 @@
namespace GitHubActionsDemo.Api.Contract; namespace GitHubActionsDemo.Api.Sdk.Authors;
public class AuthorRequest public class AuthorRequest
{ {

View file

@ -0,0 +1,10 @@
namespace GitHubActionsDemo.Api.Sdk.Authors;
public class AuthorResponse
{
public int AuthorId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
}

View file

@ -0,0 +1,16 @@
using GitHubActionsDemo.Api.Sdk.Shared;
using Refit;
namespace GitHubActionsDemo.Api.Sdk.Authors;
public interface IAuthorApi
{
[Get("/authors/")]
Task<PagedResponse<AuthorResponse>> GetAuthorsAsync();
[Get("/authors/{authorId}")]
Task<AuthorResponse> GetAuthorAsync(int authorId);
[Post("/authors/")]
Task<AuthorResponse> CreateAuthorAsync([Body] AuthorRequest request);
}

View file

@ -1,4 +1,4 @@
namespace GitHubActionsDemo.Api.Contract; namespace GitHubActionsDemo.Api.Sdk.Books;
public class BookRequest public class BookRequest
{ {

View file

@ -0,0 +1,14 @@
using GitHubActionsDemo.Api.Sdk.Authors;
namespace GitHubActionsDemo.Api.Sdk.Books;
public class BookResponse
{
public int BookId { get; set; }
public string Title { get; set; }
public AuthorResponse Author { get; set; }
public string Isbn { get; set; }
public DateTime DatePublished { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
}

View file

@ -0,0 +1,16 @@
using GitHubActionsDemo.Api.Sdk.Shared;
using Refit;
namespace GitHubActionsDemo.Api.Sdk.Books;
public interface IBookApi
{
[Get("/books/")]
Task<PagedResponse<BookResponse>> GetBooksAsync();
[Get("/books/{bookId}")]
Task<BookResponse> GetBookAsync(int bookId);
[Post("/books/")]
Task<IList<BookResponse>> CreateBookAsync([Body] BookRequest request);
}

View file

@ -1,6 +0,0 @@
namespace GitHubActionsDemo.Api.Sdk;
public class Class1
{
}

View file

@ -6,4 +6,7 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Refit" Version="7.0.0" />
</ItemGroup>
</Project> </Project>

View file

@ -1,4 +1,4 @@
namespace GitHubActionsDemo.Api.Contract; namespace GitHubActionsDemo.Api.Sdk.Shared;
public class PageRequest public class PageRequest
{ {

View file

@ -0,0 +1,16 @@
using System.Collections;
namespace GitHubActionsDemo.Api.Sdk.Shared;
public class PagedResponse<T> where T : class
{
public int Page { get; set; }
public int PageSize { get; set; }
public int Count
{
get
{
return Result.Count;
}
}
public IList<T> Result { get; set; }
}

View file

@ -1,4 +1,5 @@
using GitHubActionsDemo.Api.Contract; using GitHubActionsDemo.Api.Sdk.Authors;
using GitHubActionsDemo.Api.Sdk.Shared;
using GitHubActionsDemo.Api.Mappers; using GitHubActionsDemo.Api.Mappers;
using GitHubActionsDemo.Service; using GitHubActionsDemo.Service;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;

View file

@ -1,6 +1,6 @@
using System.Collections; using System.Collections;
using System.Net; using System.Net;
using GitHubActionsDemo.Api.Models; using GitHubActionsDemo.Api.Sdk.Shared;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace GitHubActionsDemo.Api.Controllers; namespace GitHubActionsDemo.Api.Controllers;
@ -12,8 +12,13 @@ public class BaseController : ControllerBase
return Results.StatusCode((int)HttpStatusCode.InternalServerError); return Results.StatusCode((int)HttpStatusCode.InternalServerError);
} }
public IResult PagedResult<T>(int page, int pageSize, T result) where T : IList public IResult PagedResult<T>(int page, int pageSize, IList<T> result) where T : class
{ {
return Results.Ok(new PagedResponse<T>(page, pageSize, result)); return Results.Ok(new PagedResponse<T>
{
Page = page,
PageSize = pageSize,
Result = result
});
} }
} }

View file

@ -1,4 +1,5 @@
using GitHubActionsDemo.Api.Contract; using GitHubActionsDemo.Api.Sdk.Books;
using GitHubActionsDemo.Api.Sdk.Shared;
using GitHubActionsDemo.Api.Mappers; using GitHubActionsDemo.Api.Mappers;
using GitHubActionsDemo.Service; using GitHubActionsDemo.Service;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;

View file

@ -19,7 +19,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\GitHubActionsDemo.Api.Contract\GitHubActionsDemo.Api.Contract.csproj" /> <ProjectReference Include="..\GitHubActionsDemo.Api.Sdk\GitHubActionsDemo.Api.Sdk.csproj" />
<ProjectReference Include="..\GitHubActionsDemo.Service\GitHubActionsDemo.Service.csproj" /> <ProjectReference Include="..\GitHubActionsDemo.Service\GitHubActionsDemo.Service.csproj" />
<ProjectReference Include="..\GitHubActionsDemo.Persistance\GitHubActionsDemo.Persistance.csproj" /> <ProjectReference Include="..\GitHubActionsDemo.Persistance\GitHubActionsDemo.Persistance.csproj" />
</ItemGroup> </ItemGroup>

View file

@ -1,4 +1,4 @@
using GitHubActionsDemo.Api.Contract; using GitHubActionsDemo.Api.Sdk.Authors;
using GitHubActionsDemo.Api.Models; using GitHubActionsDemo.Api.Models;
using GitHubActionsDemo.Service.Models; using GitHubActionsDemo.Service.Models;
@ -16,12 +16,14 @@ public static class AuthorMapper
public static AuthorResponse Map(this Author author) public static AuthorResponse Map(this Author author)
{ {
return new AuthorResponse( return new AuthorResponse
author.AuthorId, {
author.FirstName, AuthorId = author.AuthorId,
author.LastName, FirstName = author.FirstName,
author.DateCreated, LastName = author.LastName,
author.DateModified DateCreated = author.DateCreated,
); DateModified = author.DateModified
};
} }
} }

View file

@ -1,4 +1,4 @@
using GitHubActionsDemo.Api.Contract; using GitHubActionsDemo.Api.Sdk.Books;
using GitHubActionsDemo.Api.Models; using GitHubActionsDemo.Api.Models;
using GitHubActionsDemo.Service.Models; using GitHubActionsDemo.Service.Models;
@ -18,14 +18,15 @@ public static class BookMapper
public static BookResponse Map(this Book book) public static BookResponse Map(this Book book)
{ {
return new BookResponse( return new BookResponse
book.BookId, {
book.Title, BookId = book.BookId,
book.Author.Map(), Title = book.Title,
book.Isbn, Author = book.Author.Map(),
book.DatePublished, Isbn = book.Isbn,
book.DateCreated, DatePublished = book.DatePublished,
book.DateModified DateCreated = book.DateCreated,
); DateModified = book.DateModified
};
} }
} }

View file

@ -1,25 +0,0 @@
namespace GitHubActionsDemo.Api.Models;
public class AuthorResponse
{
public AuthorResponse(
int authorId,
string firstName,
string lastName,
DateTime dateCreated,
DateTime dateModified
)
{
AuthorId = authorId;
FirstName = firstName;
LastName = lastName;
DateCreated = dateCreated;
DateModified = dateModified;
}
public int AuthorId { get; }
public string FirstName { get; }
public string LastName { get; }
public DateTime DateCreated { get; }
public DateTime DateModified { get; }
}

View file

@ -1,31 +0,0 @@
namespace GitHubActionsDemo.Api.Models;
public class BookResponse
{
public BookResponse(
int bookId,
string title,
AuthorResponse author,
string isbn,
DateTime datePublished,
DateTime dateCreated,
DateTime dateModified
)
{
BookId = bookId;
Title = title;
Author = author;
Isbn = isbn;
DatePublished = datePublished;
DateCreated = dateCreated;
DateModified = dateModified;
}
public int BookId { get; }
public string Title { get; }
public AuthorResponse Author { get; }
public string Isbn { get; }
public DateTime DatePublished { get; }
public DateTime DateCreated { get; }
public DateTime DateModified { get; }
}

View file

@ -1,18 +0,0 @@
using System.Collections;
namespace GitHubActionsDemo.Api.Models;
public class PagedResponse<T> where T : IList
{
public PagedResponse(int page, int pageSize, T result)
{
Page = page;
PageSize = pageSize;
Result = result;
Count = result.Count;
}
public int Page { get; }
public int PageSize { get; }
public int Count { get; }
public T Result { get; }
}

View file

@ -1,5 +1,5 @@
using FluentValidation; using FluentValidation;
using GitHubActionsDemo.Api.Contract; using GitHubActionsDemo.Api.Sdk.Authors;
namespace GitHubActionsDemo.Api.Models.Validators; namespace GitHubActionsDemo.Api.Models.Validators;
public class AuthorRequestValidator : AbstractValidator<AuthorRequest> public class AuthorRequestValidator : AbstractValidator<AuthorRequest>

View file

@ -1,5 +1,5 @@
using FluentValidation; using FluentValidation;
using GitHubActionsDemo.Api.Contract; using GitHubActionsDemo.Api.Sdk.Books;
namespace GitHubActionsDemo.Api.Models.Validators; namespace GitHubActionsDemo.Api.Models.Validators;
public class BookRequestValidator : AbstractValidator<BookRequest> public class BookRequestValidator : AbstractValidator<BookRequest>

View file

@ -1,5 +1,5 @@
using FluentValidation; using FluentValidation;
using GitHubActionsDemo.Api.Contract; using GitHubActionsDemo.Api.Sdk.Shared;
namespace GitHubActionsDemo.Api.Models.Validators; namespace GitHubActionsDemo.Api.Models.Validators;
public class PageParametersValidator : AbstractValidator<PageRequest> public class PageParametersValidator : AbstractValidator<PageRequest>

View file

@ -2,7 +2,9 @@ using GitHubActionsDemo.Service.Infrastructure;
using GitHubActionsDemo.Persistance.Infrastructure; using GitHubActionsDemo.Persistance.Infrastructure;
using Serilog; using Serilog;
using FluentValidation; using FluentValidation;
using GitHubActionsDemo.Api.Contract; using GitHubActionsDemo.Api.Sdk.Authors;
using GitHubActionsDemo.Api.Sdk.Books;
using GitHubActionsDemo.Api.Sdk.Shared;
using GitHubActionsDemo.Api.Models.Validators; using GitHubActionsDemo.Api.Models.Validators;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);

View file

@ -1,12 +1,12 @@
using System.Net; using System.Net;
using FluentValidation; using FluentValidation;
using FluentValidation.Results; using FluentValidation.Results;
using GitHubActionsDemo.Api.Contract;
using GitHubActionsDemo.Api.Controllers; using GitHubActionsDemo.Api.Controllers;
using GitHubActionsDemo.Api.Models;
using GitHubActionsDemo.Service; using GitHubActionsDemo.Service;
using GitHubActionsDemo.Service.Models; using GitHubActionsDemo.Service.Models;
using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Http.HttpResults;
using GitHubActionsDemo.Api.Sdk.Authors;
using GitHubActionsDemo.Api.Sdk.Shared;
namespace GitHubActionsDemo.Api.Tests; namespace GitHubActionsDemo.Api.Tests;