Skip to content

Erlend Skutlaberg #103

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,6 @@ FodyWeavers.xsd
*/**/obj/Release
*/Migrations
/exercise.wwwapi/appsettings.json

/exercise.wwwapi/appsettings.Development.json

25 changes: 25 additions & 0 deletions exercise.webapi/DTO/AuthorDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using exercise.webapi.Models;

namespace exercise.webapi.DTO;

public class AuthorDTO
{
public int Id {get; set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public string Email {get;set;}
public ICollection<Tuple<string, string>> PublisherBooks {get;set;} = new List<Tuple<string, string>>();

public AuthorDTO(Author author)
{
Id = author.Id;
FirstName = author.FirstName;
LastName = author.LastName;
Email = author.Email;
foreach (Book b in author.Books)
{
PublisherBooks.Add(new Tuple<string, string>(b.Title, b.Publisher.Name));
}
}
}
21 changes: 21 additions & 0 deletions exercise.webapi/DTO/AuthorGetBookDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using exercise.webapi.Models;

namespace exercise.webapi.DTO;

public class AuthorGetBookDTO
{

public int Id {get; set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public string Email {get;set;}

public AuthorGetBookDTO(Author author)
{
Id = author.Id;
FirstName = author.FirstName;
LastName = author.LastName;
Email = author.Email;
}
}
8 changes: 8 additions & 0 deletions exercise.webapi/DTO/AuthorPostDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace exercise.webapi.DTO;

public class AuthorPostDTO
{
public int Id {get; set;}
}
28 changes: 28 additions & 0 deletions exercise.webapi/DTO/BookDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using exercise.webapi.Models;

namespace exercise.webapi.DTO;

public class BookDTO
{
public int Id { get; set; }
public string Title { get; set; }

public int AuthorId { get; set; }
public string PublisherName {get; set;}
public AuthorGetBookDTO Author { get; set; }


public BookDTO(Book book)
{
Id = book.Id;
Title = book.Title;
AuthorId = book.AuthorId;
PublisherName = book.Publisher.Name;
Author = new AuthorGetBookDTO(book.Author);




}
}
16 changes: 16 additions & 0 deletions exercise.webapi/DTO/BookGetAuthorDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using exercise.webapi.Models;

namespace exercise.webapi.DTO;

public class BookGetAuthorDTO
{
public int Id {get;set;}
public string Title {get;set;}

public BookGetAuthorDTO(Book b)
{
Id = b.Id;
Title = b.Title;
}
}
10 changes: 10 additions & 0 deletions exercise.webapi/DTO/CreateBookDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace exercise.webapi.DTO;

public class CreateBookDTO
{
public string Title { get; set; }

public int AuthorId { get; set; }
}
23 changes: 23 additions & 0 deletions exercise.webapi/DTO/PublisherGetDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using exercise.webapi.Models;

namespace exercise.webapi.DTO;

public class PublisherGetDTO
{

public int Id {get;set;}
public string Name {get;set;}
public List<BookDTO> Books {get;set;} = new List<BookDTO>();


public PublisherGetDTO(Publisher p)
{
Id = p.Id;
Name = p.Name;
foreach (Book b in p.Books)
{
Books.Add(new BookDTO(b));
}
}
}
2 changes: 2 additions & 0 deletions exercise.webapi/Data/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

modelBuilder.Entity<Author>().HasData(seeder.Authors);
modelBuilder.Entity<Book>().HasData(seeder.Books);
modelBuilder.Entity<Publisher>().HasData(seeder.Publishers);

}
public DbSet<Author> Authors { get; set; }
public DbSet<Book> Books { get; set; }
public DbSet<Publisher> Publishers {get;set;}
}
}
19 changes: 18 additions & 1 deletion exercise.webapi/Data/Seeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,29 @@ public class Seeder
"Leopards"
};

private List<string> _publisher = new List<string>()
{
"Hot Taco", "Beer", "Hotsauce inc.", "Git Gabble"
};

private List<Author> _authors = new List<Author>();
private List<Book> _books = new List<Book>();
private List<Publisher> _publishers = new List<Publisher>();

public Seeder()
{

Random authorRandom = new Random();
Random bookRandom = new Random();
Random publisherRandom = new Random();


for (int z = 1; z < 50; z++)
{
Publisher p = new Publisher();
p.Id = z;
p.Name = $"{_firstword[publisherRandom.Next(_firstword.Count)]} {_publisher[publisherRandom.Next(_publisher.Count)]}";
_publishers.Add(p);
}

for (int x = 1; x < 250; x++)
{
Expand All @@ -105,13 +118,17 @@ public Seeder()
book.Id = y;
book.Title = $"{_firstword[bookRandom.Next(_firstword.Count)]} {_secondword[bookRandom.Next(_secondword.Count)]} {_thirdword[bookRandom.Next(_thirdword.Count)]}";
book.AuthorId = _authors[authorRandom.Next(_authors.Count)].Id;
book.PublisherId = _publishers[publisherRandom.Next(_publishers.Count)].Id;
//book.Author = authors[book.AuthorId-1];
_books.Add(book);
}




}
public List<Author> Authors { get { return _authors; } }
public List<Book> Books { get { return _books; } }
public List<Publisher> Publishers {get {return _publishers;}}
}
}
43 changes: 43 additions & 0 deletions exercise.webapi/Endpoints/AuthorApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using exercise.webapi.DTO;
using exercise.webapi.Models;
using exercise.webapi.Payload;
using exercise.webapi.Repository;
using Microsoft.AspNetCore.Mvc;

namespace exercise.webapi.Endpoints;

public static class AuthorApi
{

public static void ConfigureAuthorApi(this WebApplication app)
{
var author = app.MapGroup("/author");

author.MapGet("/{id}", GetAuthor);
author.MapGet("/", GetAuthors);


}
[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> GetAuthor(IAuthorRepository authorRepository, int id)
{
Payload<AuthorDTO> payload = await authorRepository.GetAuthor(id);

if (!payload.GoodResponse)
{
return TypedResults.NotFound(payload.Message);
}

return TypedResults.Ok(payload.Data);
}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public static async Task<IResult> GetAuthors(IAuthorRepository authorRepository)
{
Payload<IEnumerable<AuthorDTO>> payload = await authorRepository.GetAuthors();

return TypedResults.Ok(payload.Data);
}
}
75 changes: 71 additions & 4 deletions exercise.webapi/Endpoints/BookApi.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using exercise.webapi.Models;
using exercise.webapi.DTO;
using exercise.webapi.Models;
using exercise.webapi.Payload;
using exercise.webapi.Repository;
using Microsoft.AspNetCore.Mvc;
using static System.Reflection.Metadata.BlobBuilder;

namespace exercise.webapi.Endpoints
Expand All @@ -8,13 +11,77 @@ public static class BookApi
{
public static void ConfigureBooksApi(this WebApplication app)
{
app.MapGet("/books", GetBooks);
var books = app.MapGroup("/books");
books.MapGet("/", GetBooks);
books.MapGet("/{id}", GetBook);
books.MapPut("/{id}", UpdateAuthor);
books.MapDelete("/{id}", DeleteBook);
books.MapPost("/", CreateBook);
}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
private static async Task<IResult> GetBooks(IBookRepository bookRepository)
{
var books = await bookRepository.GetAllBooks();
return TypedResults.Ok(books);
Payload<IEnumerable<BookDTO>> payload = await bookRepository.GetAllBooks();
return TypedResults.Ok(payload.Data);
}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
private static async Task<IResult> GetBook(IBookRepository bookRepository, int id)
{
Payload<BookDTO> payload = await bookRepository.GetBook(id);
if (!payload.GoodResponse)
{
return TypedResults.NotFound(payload.Message);
}

return TypedResults.Ok(payload.Data);
}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
private static async Task<IResult> UpdateAuthor(IBookRepository bookRepository, int bookId, AuthorPostDTO authorPost)
{
Payload<BookDTO> payload = await bookRepository.UpdateAuthor(bookId, authorPost);

if (!payload.GoodResponse)
{
return TypedResults.NotFound(payload.Message);
}

return TypedResults.Ok(payload.Data);
}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
private static async Task<IResult> DeleteBook(IBookRepository bookRepository, int id)
{
Payload<BookDTO> payload = await bookRepository.DeleteBook(id);

if (!payload.GoodResponse)
{
return TypedResults.NotFound(payload.Message);
}

return TypedResults.Ok(payload.Data);
}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
private static async Task<IResult> CreateBook(IBookRepository bookRepository, CreateBookDTO createBookDTO)
{
Payload<BookDTO> payload = await bookRepository.CreateBook(createBookDTO);

if (!payload.GoodResponse)
{
return TypedResults.NotFound(payload.Message);
}

return TypedResults.Ok(payload.Data);


}
}
}
51 changes: 51 additions & 0 deletions exercise.webapi/Endpoints/PublisherAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Xml.Serialization;
using exercise.webapi.DTO;
using exercise.webapi.Payload;
using exercise.webapi.Repository;
using Microsoft.AspNetCore.Mvc;

namespace exercise.webapi.Endpoints;

public static class PublisherAPI
{

public static void ConfigurePublisherApi(this WebApplication app)
{
var publisher = app.MapGroup("/publishers");

publisher.MapGet("/", GetPublishers);
publisher.MapGet("/{id}", GetPublisher);


}

[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> GetPublishers(IPublisherRepository publisherRepository)
{
Payload<List<PublisherGetDTO>> payload = await publisherRepository.GetPublishers();

if (!payload.GoodResponse)
{
return TypedResults.NotFound(payload.Message);
}

return TypedResults.Ok(payload.Data);

}


[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public static async Task<IResult> GetPublisher(IPublisherRepository publisherRepository, int id)
{
Payload<PublisherGetDTO> payload = await publisherRepository.GetPublisher(id);

if (!payload.GoodResponse)
{
return TypedResults.NotFound(payload.Message);
}

return TypedResults.Ok(payload.Data);
}
}
Loading