Skip to content

Lowe Raivio #110

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 4 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
23 changes: 23 additions & 0 deletions exercise.webapi/DTO/Author_get.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using exercise.webapi.Models;

namespace exercise.webapi.DTO
{
public class Author_get
{
public Author_get(Author author)
{
this.Id = author.Id;
this.FirstName = author.FirstName;
this.LastName = author.LastName;
this.Email = author.Email;
this.Books = author.Books.Select(x => new Author_get_Book(x)).ToList();
}
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }

public ICollection<Author_get_Book> Books { get; set; } = new List<Author_get_Book>();

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

namespace exercise.webapi.DTO
{
public class Author_get_Book
{
public Author_get_Book(Book book)
{
this.Id = book.Id;
this.Title = book.Title;
this.Publisher = new Book_get_Publisher(book.Publisher);
}
public int Id { get; set; }
public string Title { get; set; }
public Book_get_Publisher Publisher { get; set; }
}
}
12 changes: 12 additions & 0 deletions exercise.webapi/DTO/Book_create.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using exercise.webapi.Models;

namespace exercise.webapi.DTO
{
public class Book_create
{
public string Title { get; set; }
public int AuthorId { get; set; }
public int PublisherId { get; set; }

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

namespace exercise.webapi.DTO
{
public class Book_get
{
public Book_get(Book book)
{
this.Id = book.Id;
this.Title = book.Title;
this.Author = new Book_get_Author(book.Author);
this.Publisher = new Book_get_Publisher(book.Publisher);
}
public int Id { get; set; }
public string Title { get; set; }
public Book_get_Author Author { get; set; }
public Book_get_Publisher Publisher { get; set; }
}
}
20 changes: 20 additions & 0 deletions exercise.webapi/DTO/Book_get_Author.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using exercise.webapi.Models;

namespace exercise.webapi.DTO
{
public class Book_get_Author
{
public Book_get_Author(Author author)
{
this.Id = author.Id;
this.FirstName = author.FirstName;
this.LastName = author.LastName;
this.Email = author.Email;
}
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }

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

namespace exercise.webapi.DTO
{
public class Book_get_Publisher
{
public Book_get_Publisher(Publisher publisher)
{
this.Id = publisher.Id;
this.Name = publisher.Name;
}
public int Id { get; set; }
public string Name { get; set; }


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

namespace exercise.webapi.DTO
{
public class Book_update
{
public int AuthorId { get; set; }

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

namespace exercise.webapi.DTO
{
public class Publisher_get
{
public Publisher_get(Publisher publisher)
{
this.Id = publisher.Id;
this.Name = publisher.Name;
this.books = publisher.Books.Select(x => new Publisher_get_Book(x)).ToList();
}
public int Id { get; set; }
public string Name { get; set; }
public List<Publisher_get_Book> books { get; set; }

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

namespace exercise.webapi.DTO
{
public class Publisher_get_Book
{
public Publisher_get_Book(Book book)
{
this.Id = book.Id;
this.Title = book.Title;
this.Author = new Book_get_Author(book.Author);
}
public int Id { get; set; }
public string Title { get; set; }
public Book_get_Author Author { get; set; }
}
}
3 changes: 2 additions & 1 deletion exercise.webapi/Data/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ 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; }
}
}
58 changes: 57 additions & 1 deletion exercise.webapi/Data/Seeder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using exercise.webapi.Models;
using System.Diagnostics;
using exercise.webapi.Models;

namespace exercise.webapi.Data
{
Expand Down Expand Up @@ -76,15 +77,60 @@ public class Seeder
"Flowers",
"Leopards"
};

private List<List<string>> _publisherWords= new List<List<string>>()
{
new List<string>{
"Jumbled",
"Frantic",
"Scrambled",
"Caffeinated",
"Unraveled",
"Overloaded",
"Lost",
"Infinite",
"Radiant",
"Majestic",

},
new List<string>{
"Byte",
"Paw",
"Script",
"Code",
"Whisker",
"Studios",
"Co.",
"Works",
"Lab",
"Collective",
"Ventures",
"Agency",
"Bureau",
},
new List<string>{
"Press",
"Concepts",
"Enterprises",
"Projects",
"Publications",
"Systems",
"Industries",
"Studios",
"Solutions",
},
};

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



Expand All @@ -98,20 +144,30 @@ public Seeder()
_authors.Add(author);
}

for (int z = 1; z < 250; z++)
{
Publisher publisher = new Publisher();
publisher.Id = z;
publisher.Name = $"{_publisherWords[0][publisherRandom.Next(_publisherWords[0].Count)]} {_publisherWords[1][publisherRandom.Next(_publisherWords[1].Count)]} {_publisherWords[2][publisherRandom.Next(_publisherWords[2].Count)]}";
_publishers.Add(publisher);
}

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.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; } }
}
}
35 changes: 35 additions & 0 deletions exercise.webapi/Endpoints/AuthorApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using exercise.webapi.DTO;
using exercise.webapi.Repository;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;

namespace exercise.webapi.Endpoints
{
public static class AuthorApi
{
public static void ConfigureAuthorsApi(this WebApplication app)
{
var authors = app.MapGroup("/authors");
authors.MapGet("/", GetAuthors);
authors.MapGet("/{id}", GetAuthor);
}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
private static async Task<IResult> GetAuthors(IAuthorRepository authorRepository)
{
var authors = await authorRepository.GetAllAuthors();
if (authors.Count() == 0) return TypedResults.NotFound($"No authors found in the database");
return TypedResults.Ok(authors.Select(x => new Author_get(x)));
}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
private static async Task<IResult> GetAuthor(IAuthorRepository authorRepository, int id)
{
var author = await authorRepository.GetAuthor(id);
if (author == null) return TypedResults.NotFound($"No author with id[{id}] exists");
return TypedResults.Ok(new Author_get(author));
}
}
}
Loading