From 0cb97f48df52072901507baaf03d02ae3e492398 Mon Sep 17 00:00:00 2001 From: Alex Hyett Date: Fri, 7 Jul 2023 11:54:49 +0100 Subject: [PATCH] Add Refit SDK --- .../GitHubActionsDemo.Api.Contract.csproj | 9 ------ .../Authors}/AuthorRequest.cs | 2 +- .../Authors/AuthorResponse.cs | 10 ++++++ .../Authors/IAuthorApi.cs | 16 ++++++++++ .../Books}/BookRequest.cs | 2 +- .../Books/BookResponse.cs | 14 +++++++++ .../Books/IBookApi.cs | 16 ++++++++++ src/GitHubActionsDemo.Api.Sdk/Class1.cs | 6 ---- .../GitHubActionsDemo.Api.Sdk.csproj | 5 ++- .../Shared}/PageRequest.cs | 2 +- .../Shared/PagedResponse.cs | 16 ++++++++++ .../Controllers/AuthorsController.cs | 3 +- .../Controllers/BaseController.cs | 11 +++++-- .../Controllers/BooksController.cs | 3 +- .../GitHubActionsDemo.Api.csproj | 2 +- .../Mappers/AuthorMapper.cs | 18 ++++++----- .../Mappers/BookMapper.cs | 21 +++++++------ .../Models/AuthorResponse.cs | 25 --------------- .../Models/BookResponse.cs | 31 ------------------- .../Models/PagedResponse.cs | 18 ----------- .../Validators/AuthorRequestValidator.cs | 2 +- .../Models/Validators/BookRequestValidator.cs | 2 +- .../Validators/PageParametersValidator.cs | 2 +- src/GitHubActionsDemo.Api/Program.cs | 4 ++- .../AuthorsControllerTests.cs | 4 +-- 25 files changed, 121 insertions(+), 123 deletions(-) delete mode 100644 src/GitHubActionsDemo.Api.Contract/GitHubActionsDemo.Api.Contract.csproj rename src/{GitHubActionsDemo.Api.Contract => GitHubActionsDemo.Api.Sdk/Authors}/AuthorRequest.cs (71%) create mode 100644 src/GitHubActionsDemo.Api.Sdk/Authors/AuthorResponse.cs create mode 100644 src/GitHubActionsDemo.Api.Sdk/Authors/IAuthorApi.cs rename src/{GitHubActionsDemo.Api.Contract => GitHubActionsDemo.Api.Sdk/Books}/BookRequest.cs (78%) create mode 100644 src/GitHubActionsDemo.Api.Sdk/Books/BookResponse.cs create mode 100644 src/GitHubActionsDemo.Api.Sdk/Books/IBookApi.cs delete mode 100644 src/GitHubActionsDemo.Api.Sdk/Class1.cs rename src/{GitHubActionsDemo.Api.Contract => GitHubActionsDemo.Api.Sdk/Shared}/PageRequest.cs (71%) create mode 100644 src/GitHubActionsDemo.Api.Sdk/Shared/PagedResponse.cs delete mode 100644 src/GitHubActionsDemo.Api/Models/AuthorResponse.cs delete mode 100644 src/GitHubActionsDemo.Api/Models/BookResponse.cs delete mode 100644 src/GitHubActionsDemo.Api/Models/PagedResponse.cs diff --git a/src/GitHubActionsDemo.Api.Contract/GitHubActionsDemo.Api.Contract.csproj b/src/GitHubActionsDemo.Api.Contract/GitHubActionsDemo.Api.Contract.csproj deleted file mode 100644 index 4658cbf..0000000 --- a/src/GitHubActionsDemo.Api.Contract/GitHubActionsDemo.Api.Contract.csproj +++ /dev/null @@ -1,9 +0,0 @@ - - - - net7.0 - enable - enable - - - diff --git a/src/GitHubActionsDemo.Api.Contract/AuthorRequest.cs b/src/GitHubActionsDemo.Api.Sdk/Authors/AuthorRequest.cs similarity index 71% rename from src/GitHubActionsDemo.Api.Contract/AuthorRequest.cs rename to src/GitHubActionsDemo.Api.Sdk/Authors/AuthorRequest.cs index f41a04b..64744f9 100644 --- a/src/GitHubActionsDemo.Api.Contract/AuthorRequest.cs +++ b/src/GitHubActionsDemo.Api.Sdk/Authors/AuthorRequest.cs @@ -1,4 +1,4 @@ -namespace GitHubActionsDemo.Api.Contract; +namespace GitHubActionsDemo.Api.Sdk.Authors; public class AuthorRequest { diff --git a/src/GitHubActionsDemo.Api.Sdk/Authors/AuthorResponse.cs b/src/GitHubActionsDemo.Api.Sdk/Authors/AuthorResponse.cs new file mode 100644 index 0000000..e72f127 --- /dev/null +++ b/src/GitHubActionsDemo.Api.Sdk/Authors/AuthorResponse.cs @@ -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; } +} diff --git a/src/GitHubActionsDemo.Api.Sdk/Authors/IAuthorApi.cs b/src/GitHubActionsDemo.Api.Sdk/Authors/IAuthorApi.cs new file mode 100644 index 0000000..f461aa5 --- /dev/null +++ b/src/GitHubActionsDemo.Api.Sdk/Authors/IAuthorApi.cs @@ -0,0 +1,16 @@ +using GitHubActionsDemo.Api.Sdk.Shared; +using Refit; + +namespace GitHubActionsDemo.Api.Sdk.Authors; + +public interface IAuthorApi +{ + [Get("/authors/")] + Task> GetAuthorsAsync(); + + [Get("/authors/{authorId}")] + Task GetAuthorAsync(int authorId); + + [Post("/authors/")] + Task CreateAuthorAsync([Body] AuthorRequest request); +} diff --git a/src/GitHubActionsDemo.Api.Contract/BookRequest.cs b/src/GitHubActionsDemo.Api.Sdk/Books/BookRequest.cs similarity index 78% rename from src/GitHubActionsDemo.Api.Contract/BookRequest.cs rename to src/GitHubActionsDemo.Api.Sdk/Books/BookRequest.cs index 6b4fbcf..e089958 100644 --- a/src/GitHubActionsDemo.Api.Contract/BookRequest.cs +++ b/src/GitHubActionsDemo.Api.Sdk/Books/BookRequest.cs @@ -1,4 +1,4 @@ -namespace GitHubActionsDemo.Api.Contract; +namespace GitHubActionsDemo.Api.Sdk.Books; public class BookRequest { diff --git a/src/GitHubActionsDemo.Api.Sdk/Books/BookResponse.cs b/src/GitHubActionsDemo.Api.Sdk/Books/BookResponse.cs new file mode 100644 index 0000000..c375c09 --- /dev/null +++ b/src/GitHubActionsDemo.Api.Sdk/Books/BookResponse.cs @@ -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; } +} diff --git a/src/GitHubActionsDemo.Api.Sdk/Books/IBookApi.cs b/src/GitHubActionsDemo.Api.Sdk/Books/IBookApi.cs new file mode 100644 index 0000000..4f09cf4 --- /dev/null +++ b/src/GitHubActionsDemo.Api.Sdk/Books/IBookApi.cs @@ -0,0 +1,16 @@ +using GitHubActionsDemo.Api.Sdk.Shared; +using Refit; + +namespace GitHubActionsDemo.Api.Sdk.Books; + +public interface IBookApi +{ + [Get("/books/")] + Task> GetBooksAsync(); + + [Get("/books/{bookId}")] + Task GetBookAsync(int bookId); + + [Post("/books/")] + Task> CreateBookAsync([Body] BookRequest request); +} diff --git a/src/GitHubActionsDemo.Api.Sdk/Class1.cs b/src/GitHubActionsDemo.Api.Sdk/Class1.cs deleted file mode 100644 index f3cdf9f..0000000 --- a/src/GitHubActionsDemo.Api.Sdk/Class1.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace GitHubActionsDemo.Api.Sdk; - -public class Class1 -{ - -} diff --git a/src/GitHubActionsDemo.Api.Sdk/GitHubActionsDemo.Api.Sdk.csproj b/src/GitHubActionsDemo.Api.Sdk/GitHubActionsDemo.Api.Sdk.csproj index 4658cbf..e0cfd6c 100644 --- a/src/GitHubActionsDemo.Api.Sdk/GitHubActionsDemo.Api.Sdk.csproj +++ b/src/GitHubActionsDemo.Api.Sdk/GitHubActionsDemo.Api.Sdk.csproj @@ -5,5 +5,8 @@ enable enable - + + + + diff --git a/src/GitHubActionsDemo.Api.Contract/PageRequest.cs b/src/GitHubActionsDemo.Api.Sdk/Shared/PageRequest.cs similarity index 71% rename from src/GitHubActionsDemo.Api.Contract/PageRequest.cs rename to src/GitHubActionsDemo.Api.Sdk/Shared/PageRequest.cs index c024086..48da9b4 100644 --- a/src/GitHubActionsDemo.Api.Contract/PageRequest.cs +++ b/src/GitHubActionsDemo.Api.Sdk/Shared/PageRequest.cs @@ -1,4 +1,4 @@ -namespace GitHubActionsDemo.Api.Contract; +namespace GitHubActionsDemo.Api.Sdk.Shared; public class PageRequest { diff --git a/src/GitHubActionsDemo.Api.Sdk/Shared/PagedResponse.cs b/src/GitHubActionsDemo.Api.Sdk/Shared/PagedResponse.cs new file mode 100644 index 0000000..81e1b93 --- /dev/null +++ b/src/GitHubActionsDemo.Api.Sdk/Shared/PagedResponse.cs @@ -0,0 +1,16 @@ +using System.Collections; +namespace GitHubActionsDemo.Api.Sdk.Shared; + +public class PagedResponse where T : class +{ + public int Page { get; set; } + public int PageSize { get; set; } + public int Count + { + get + { + return Result.Count; + } + } + public IList Result { get; set; } +} \ No newline at end of file diff --git a/src/GitHubActionsDemo.Api/Controllers/AuthorsController.cs b/src/GitHubActionsDemo.Api/Controllers/AuthorsController.cs index 3ee84d2..a796c61 100644 --- a/src/GitHubActionsDemo.Api/Controllers/AuthorsController.cs +++ b/src/GitHubActionsDemo.Api/Controllers/AuthorsController.cs @@ -1,4 +1,5 @@ -using GitHubActionsDemo.Api.Contract; +using GitHubActionsDemo.Api.Sdk.Authors; +using GitHubActionsDemo.Api.Sdk.Shared; using GitHubActionsDemo.Api.Mappers; using GitHubActionsDemo.Service; using Microsoft.AspNetCore.Mvc; diff --git a/src/GitHubActionsDemo.Api/Controllers/BaseController.cs b/src/GitHubActionsDemo.Api/Controllers/BaseController.cs index 7cb2fe8..5c3904c 100644 --- a/src/GitHubActionsDemo.Api/Controllers/BaseController.cs +++ b/src/GitHubActionsDemo.Api/Controllers/BaseController.cs @@ -1,6 +1,6 @@ using System.Collections; using System.Net; -using GitHubActionsDemo.Api.Models; +using GitHubActionsDemo.Api.Sdk.Shared; using Microsoft.AspNetCore.Mvc; namespace GitHubActionsDemo.Api.Controllers; @@ -12,8 +12,13 @@ public class BaseController : ControllerBase return Results.StatusCode((int)HttpStatusCode.InternalServerError); } - public IResult PagedResult(int page, int pageSize, T result) where T : IList + public IResult PagedResult(int page, int pageSize, IList result) where T : class { - return Results.Ok(new PagedResponse(page, pageSize, result)); + return Results.Ok(new PagedResponse + { + Page = page, + PageSize = pageSize, + Result = result + }); } } \ No newline at end of file diff --git a/src/GitHubActionsDemo.Api/Controllers/BooksController.cs b/src/GitHubActionsDemo.Api/Controllers/BooksController.cs index edab700..41c679e 100644 --- a/src/GitHubActionsDemo.Api/Controllers/BooksController.cs +++ b/src/GitHubActionsDemo.Api/Controllers/BooksController.cs @@ -1,4 +1,5 @@ -using GitHubActionsDemo.Api.Contract; +using GitHubActionsDemo.Api.Sdk.Books; +using GitHubActionsDemo.Api.Sdk.Shared; using GitHubActionsDemo.Api.Mappers; using GitHubActionsDemo.Service; using Microsoft.AspNetCore.Mvc; diff --git a/src/GitHubActionsDemo.Api/GitHubActionsDemo.Api.csproj b/src/GitHubActionsDemo.Api/GitHubActionsDemo.Api.csproj index 89cf809..983475e 100644 --- a/src/GitHubActionsDemo.Api/GitHubActionsDemo.Api.csproj +++ b/src/GitHubActionsDemo.Api/GitHubActionsDemo.Api.csproj @@ -19,7 +19,7 @@ - + diff --git a/src/GitHubActionsDemo.Api/Mappers/AuthorMapper.cs b/src/GitHubActionsDemo.Api/Mappers/AuthorMapper.cs index ad925af..b3be853 100644 --- a/src/GitHubActionsDemo.Api/Mappers/AuthorMapper.cs +++ b/src/GitHubActionsDemo.Api/Mappers/AuthorMapper.cs @@ -1,4 +1,4 @@ -using GitHubActionsDemo.Api.Contract; +using GitHubActionsDemo.Api.Sdk.Authors; using GitHubActionsDemo.Api.Models; using GitHubActionsDemo.Service.Models; @@ -16,12 +16,14 @@ public static class AuthorMapper public static AuthorResponse Map(this Author author) { - return new AuthorResponse( - author.AuthorId, - author.FirstName, - author.LastName, - author.DateCreated, - author.DateModified - ); + return new AuthorResponse + { + AuthorId = author.AuthorId, + FirstName = author.FirstName, + LastName = author.LastName, + DateCreated = author.DateCreated, + DateModified = author.DateModified + }; + } } \ No newline at end of file diff --git a/src/GitHubActionsDemo.Api/Mappers/BookMapper.cs b/src/GitHubActionsDemo.Api/Mappers/BookMapper.cs index 92ffbef..efdc163 100644 --- a/src/GitHubActionsDemo.Api/Mappers/BookMapper.cs +++ b/src/GitHubActionsDemo.Api/Mappers/BookMapper.cs @@ -1,4 +1,4 @@ -using GitHubActionsDemo.Api.Contract; +using GitHubActionsDemo.Api.Sdk.Books; using GitHubActionsDemo.Api.Models; using GitHubActionsDemo.Service.Models; @@ -18,14 +18,15 @@ public static class BookMapper public static BookResponse Map(this Book book) { - return new BookResponse( - book.BookId, - book.Title, - book.Author.Map(), - book.Isbn, - book.DatePublished, - book.DateCreated, - book.DateModified - ); + return new BookResponse + { + BookId = book.BookId, + Title = book.Title, + Author = book.Author.Map(), + Isbn = book.Isbn, + DatePublished = book.DatePublished, + DateCreated = book.DateCreated, + DateModified = book.DateModified + }; } } \ No newline at end of file diff --git a/src/GitHubActionsDemo.Api/Models/AuthorResponse.cs b/src/GitHubActionsDemo.Api/Models/AuthorResponse.cs deleted file mode 100644 index 2c04b51..0000000 --- a/src/GitHubActionsDemo.Api/Models/AuthorResponse.cs +++ /dev/null @@ -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; } -} diff --git a/src/GitHubActionsDemo.Api/Models/BookResponse.cs b/src/GitHubActionsDemo.Api/Models/BookResponse.cs deleted file mode 100644 index afc0dcd..0000000 --- a/src/GitHubActionsDemo.Api/Models/BookResponse.cs +++ /dev/null @@ -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; } -} diff --git a/src/GitHubActionsDemo.Api/Models/PagedResponse.cs b/src/GitHubActionsDemo.Api/Models/PagedResponse.cs deleted file mode 100644 index 8125a9c..0000000 --- a/src/GitHubActionsDemo.Api/Models/PagedResponse.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System.Collections; -namespace GitHubActionsDemo.Api.Models; - -public class PagedResponse 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; } -} \ No newline at end of file diff --git a/src/GitHubActionsDemo.Api/Models/Validators/AuthorRequestValidator.cs b/src/GitHubActionsDemo.Api/Models/Validators/AuthorRequestValidator.cs index 65f41a8..d289c93 100644 --- a/src/GitHubActionsDemo.Api/Models/Validators/AuthorRequestValidator.cs +++ b/src/GitHubActionsDemo.Api/Models/Validators/AuthorRequestValidator.cs @@ -1,5 +1,5 @@ using FluentValidation; -using GitHubActionsDemo.Api.Contract; +using GitHubActionsDemo.Api.Sdk.Authors; namespace GitHubActionsDemo.Api.Models.Validators; public class AuthorRequestValidator : AbstractValidator diff --git a/src/GitHubActionsDemo.Api/Models/Validators/BookRequestValidator.cs b/src/GitHubActionsDemo.Api/Models/Validators/BookRequestValidator.cs index c77d2ff..7c8288b 100644 --- a/src/GitHubActionsDemo.Api/Models/Validators/BookRequestValidator.cs +++ b/src/GitHubActionsDemo.Api/Models/Validators/BookRequestValidator.cs @@ -1,5 +1,5 @@ using FluentValidation; -using GitHubActionsDemo.Api.Contract; +using GitHubActionsDemo.Api.Sdk.Books; namespace GitHubActionsDemo.Api.Models.Validators; public class BookRequestValidator : AbstractValidator diff --git a/src/GitHubActionsDemo.Api/Models/Validators/PageParametersValidator.cs b/src/GitHubActionsDemo.Api/Models/Validators/PageParametersValidator.cs index 2a09bca..b076930 100644 --- a/src/GitHubActionsDemo.Api/Models/Validators/PageParametersValidator.cs +++ b/src/GitHubActionsDemo.Api/Models/Validators/PageParametersValidator.cs @@ -1,5 +1,5 @@ using FluentValidation; -using GitHubActionsDemo.Api.Contract; +using GitHubActionsDemo.Api.Sdk.Shared; namespace GitHubActionsDemo.Api.Models.Validators; public class PageParametersValidator : AbstractValidator diff --git a/src/GitHubActionsDemo.Api/Program.cs b/src/GitHubActionsDemo.Api/Program.cs index c7ef2cb..a78af57 100644 --- a/src/GitHubActionsDemo.Api/Program.cs +++ b/src/GitHubActionsDemo.Api/Program.cs @@ -2,7 +2,9 @@ using GitHubActionsDemo.Service.Infrastructure; using GitHubActionsDemo.Persistance.Infrastructure; using Serilog; 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; var builder = WebApplication.CreateBuilder(args); diff --git a/test/GitHubActionsDemo.Api.Tests/AuthorsControllerTests.cs b/test/GitHubActionsDemo.Api.Tests/AuthorsControllerTests.cs index 4da9dc0..de39f22 100644 --- a/test/GitHubActionsDemo.Api.Tests/AuthorsControllerTests.cs +++ b/test/GitHubActionsDemo.Api.Tests/AuthorsControllerTests.cs @@ -1,12 +1,12 @@ using System.Net; using FluentValidation; using FluentValidation.Results; -using GitHubActionsDemo.Api.Contract; using GitHubActionsDemo.Api.Controllers; -using GitHubActionsDemo.Api.Models; using GitHubActionsDemo.Service; using GitHubActionsDemo.Service.Models; using Microsoft.AspNetCore.Http.HttpResults; +using GitHubActionsDemo.Api.Sdk.Authors; +using GitHubActionsDemo.Api.Sdk.Shared; namespace GitHubActionsDemo.Api.Tests;