Skip to content

Kristian Sylte #95

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 1 commit 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
16 changes: 8 additions & 8 deletions exercise.webapi/Data/DataContext.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
using exercise.webapi.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Reflection.Emit;
using exercise.webapi.Models;
using Microsoft.EntityFrameworkCore;

namespace exercise.webapi.Data
{
public class DataContext : DbContext
{
public DataContext(DbContextOptions<DataContext> options)
: base(options) { }

public DataContext(DbContextOptions<DataContext> options) : base(options)
{

}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
Expand All @@ -24,9 +22,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

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

modelBuilder.Entity<Publisher>().HasData(seeder.Publisher);
}

public DbSet<Author> Authors { get; set; }
public DbSet<Book> Books { get; set; }
public DbSet<Publisher> Publisher { get; set; }
}
}
48 changes: 31 additions & 17 deletions exercise.webapi/Data/Seeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Seeder
"Mick",
"Kate",
"Charles",
"Kate"
"Kate",
};
private List<string> _lastnames = new List<string>()
{
Expand All @@ -28,8 +28,7 @@ public class Seeder
"Jagger",
"Winslet",
"Windsor",
"Middleton"

"Middleton",
};
private List<string> _domain = new List<string>()
{
Expand All @@ -42,7 +41,7 @@ public class Seeder
"gov.us",
"gov.gr",
"gov.nl",
"gov.ru"
"gov.ru",
};
private List<string> _firstword = new List<string>()
{
Expand All @@ -52,9 +51,7 @@ public class Seeder
"Fifteen",
"A bunch of",
"An army of",
"A herd of"


"A herd of",
};
private List<string> _secondword = new List<string>()
{
Expand All @@ -65,7 +62,7 @@ public class Seeder
"Green",
"Transparent",
"Rose Smelling",
"Bitter"
"Bitter",
};
private List<string> _thirdword = new List<string>()
{
Expand All @@ -74,44 +71,61 @@ public class Seeder
"Planets",
"Houses",
"Flowers",
"Leopards"
"Leopards",
};

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();


for (int i = 1; i < 30; i++)
{
Publisher publisher = new Publisher();
publisher.Id = i;
publisher.Name = $"publisher {i}";
_publishers.Add(publisher);
}

for (int x = 1; x < 250; x++)
{
Author author = new Author();
author.Id = x;
author.FirstName = _firstnames[authorRandom.Next(_firstnames.Count)];
author.LastName = _lastnames[authorRandom.Next(_lastnames.Count)];
author.Email = $"{author.FirstName}.{author.LastName}@{_domain[authorRandom.Next(_domain.Count)]}".ToLower();
author.Email =
$"{author.FirstName}.{author.LastName}@{_domain[authorRandom.Next(_domain.Count)]}".ToLower();
_authors.Add(author);
}


for (int y = 1; y < 250; y++)
{
Book book = new Book();
book.Id = y;
book.Title = $"{_firstword[bookRandom.Next(_firstword.Count)]} {_secondword[bookRandom.Next(_secondword.Count)]} {_thirdword[bookRandom.Next(_thirdword.Count)]}";
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[authorRandom.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> Publisher
{
get { return _publishers; }
}
public List<Author> Authors { get { return _authors; } }
public List<Book> Books { get { return _books; } }
}
}
31 changes: 31 additions & 0 deletions exercise.webapi/Endpoints/AuthorAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using exercise.webapi.Models;
using exercise.webapi.Repository;
using static System.Reflection.Metadata.BlobBuilder;

namespace exercise.webapi.Endpoints;

public static class AuthorApi
{
public static void ConfigureAuthorsApi(this WebApplication app)
{
app.MapGet("/author", GetAll);
app.MapGet("/author/{id}", Get);
}

private static async Task<IResult> GetAll(IAuthorRepository repo)
{
var result = await repo.GetAll();
var authors = result.Select(a => new AuthorDTO(a));
return TypedResults.Ok(authors);
}

private static async Task<IResult> Get(IAuthorRepository repo, int id)
{
var result = await repo.Get(id);
if (result == null)
{
return TypedResults.NotFound("Author with given id not found");
}
return TypedResults.Ok(new AuthorDTO(result));
}
}
67 changes: 66 additions & 1 deletion exercise.webapi/Endpoints/BookApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,77 @@ public static class BookApi
public static void ConfigureBooksApi(this WebApplication app)
{
app.MapGet("/books", GetBooks);
app.MapGet("/books/{id}", GetBook);
app.MapGet("/books/delete/{id}", DeleteBook);
app.MapPost("/books/update", UpdateBook);
app.MapPost("/books/add", CreateBook);
}

private static async Task<IResult> GetBooks(IBookRepository bookRepository)
{
var books = await bookRepository.GetAllBooks();
return TypedResults.Ok(books);
return TypedResults.Ok(books.Select(b => new BookDTO(b)));
}

private static async Task<IResult> GetBook(IBookRepository bookRepository, int id)
{
var book = await bookRepository.GetBook(id);
if (book == null)
{
return TypedResults.NotFound("No book with give id");
}
return TypedResults.Ok(new BookDTO(book));
}

private static async Task<IResult> CreateBook(
IBookRepository bookRepository,
BookPostDTO book
)
{
try
{
var result = await bookRepository.CreateBook(
new Book
{
AuthorId = book.AuthorId,
Title = book.Title,
PublisherId = book.PublisherId,
}
);
if (result == null)
{
return TypedResults.NotFound("Author or publisher not found");
}
return TypedResults.Ok(new BookDTO(result));
}
catch (Exception e)
{
return TypedResults.BadRequest(e);
}
}

private static async Task<IResult> DeleteBook(IBookRepository repository, int id)
{
var deleted = await repository.DeleteBook(id);
if (!deleted)
{
return TypedResults.NotFound("No book with given id");
}
return TypedResults.Ok();
}

private static async Task<IResult> UpdateBook(
IBookRepository repo,
int bookId,
int authorId
)
{
var result = await repo.UpdateAuthor(bookId, authorId);
if (result == null)
{
return TypedResults.NotFound("Book or author with given id does not exist");
}
return TypedResults.Ok(new BookDTO(result));
}
}
}
31 changes: 31 additions & 0 deletions exercise.webapi/Endpoints/PublisherApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using exercise.webapi.Models;
using exercise.webapi.Repository;
using static System.Reflection.Metadata.BlobBuilder;

namespace exercise.webapi.Endpoints;

public static class PublisherApi
{
public static void ConfigurePublisherApi(this WebApplication app)
{
app.MapGet("/publisher", GetAll);
app.MapGet("/publisher/{id}", Get);
}

private static async Task<IResult> GetAll(IPublisherRepository repo)
{
var result = await repo.GetAll();
var authors = result.Select(p => new PublisherDTO(p));
return TypedResults.Ok(authors);
}

private static async Task<IResult> Get(IPublisherRepository repo, int id)
{
var result = await repo.Get(id);
if (result == null)
{
return TypedResults.NotFound("Publisher with given id not found");
}
return TypedResults.Ok(new PublisherDTO(result));
}
}
1 change: 0 additions & 1 deletion exercise.webapi/Models/Author.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public class Author
public string LastName { get; set; }
public string Email { get; set; }

[JsonIgnore] // Todo: replace this with DTO approach
public ICollection<Book> Books { get; set; } = new List<Book>();
}
}
36 changes: 36 additions & 0 deletions exercise.webapi/Models/AuthorDTOs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace exercise.webapi.Models;

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<BookStrippedDTO> Books { get; set; } = new List<BookStrippedDTO>();

public AuthorDTO(Author author)
{
this.Id = author.Id;
this.FirstName = author.FirstName;
this.LastName = author.LastName;
this.Email = author.Email;
this.Books = author.Books.Select(b => new BookStrippedDTO(b)).ToList();
}
}

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

public AuthorStrippedDTO(Author author)
{
this.Id = author.Id;
this.FirstName = author.FirstName;
this.LastName = author.LastName;
this.Email = author.Email;
}
}
20 changes: 11 additions & 9 deletions exercise.webapi/Models/Book.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System.ComponentModel.DataAnnotations.Schema;

namespace exercise.webapi.Models
namespace exercise.webapi.Models;

public class Book
{
public class Book
{
public int Id { get; set; }
public string Title { get; set; }
public int AuthorId { get; set; }
public Author Author { get; set; }
}
public int Id { get; set; }
public string Title { get; set; }

public int PublisherId { get; set; }
public Publisher Publisher { get; set; }

public int AuthorId { get; set; }
public Author Author { get; set; }
}
Loading