Skip to content
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
################################################################################
/.vs
/.idea
/web-library/appsettings.json
/web-library/bin
/web-library/obj
/web-library/web-library.csproj.user
Expand Down
52 changes: 22 additions & 30 deletions web-library/Book/Controller/BookController.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,58 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace web_library.Book.Controller
{
using Entity;
using web_library.Book.Request;
using web_library.Book.Service;
using Request;
using Service;
using DataProvider;
using Microsoft.AspNetCore.Authorization;

[Route("api/[controller]")]
[ApiController]
public class BookController : ControllerBase
{
private readonly IBookService _bookService;
private readonly IBookDataProvider _bookDataProvider;

public BookController(IBookService bookService)
public BookController(IBookService bookService, IBookDataProvider bookDataProvider)
{
_bookService = bookService;
_bookDataProvider = bookDataProvider;
}

// GET: api/<ValuesController>
[HttpGet]
public ActionResult<IEnumerable<Book>> Get()
{
return Ok();
}

// GET api/<ValuesController>/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
return Ok(_bookDataProvider.getAll());
}

// POST api/<ValuesController>
[HttpPost]
public ActionResult Post([FromBody] CreateBookRequest request)
{
try
{
_bookService.createBook(request);
}
catch
(Exception)
{
return NotFound();
}
_bookService.createBook(request);
return Ok();
}

// PUT api/<ValuesController>/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
// POST api/<ValuesController>
[HttpPost("seed")]
public ActionResult Seed([FromBody] CreateBookRequest request)
{
request = new("seed");
_bookService.createBook(request);
return Ok();
}

// DELETE api/<ValuesController>/5
[HttpDelete("{id}")]
public void Delete(int id)
// PUT api/<ValuesController>/5
[HttpPut("{id}")]
public ActionResult Put(int id, [FromBody] AssigneGenreToBookRequest request)
{
request.book_id = id;
_bookService.assigneGenre(request);
return Ok();
}
}
}
40 changes: 40 additions & 0 deletions web-library/Book/DataProvider/BookDataProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace web_library.Book.DataProvider;
using Entity;
using web_library.Book.Model;
using web_library.Book.Repository;

public class BookDataProvider : IBookDataProvider
{
private readonly IBookRepository _bookRepository;
public BookDataProvider(IBookRepository bookRepository)
{
_bookRepository = bookRepository;
}
private BookModel getModel(Book entity)
{
return new BookModel(
entity.Id,
entity.ISBN,
entity.Title,
entity.Author,
entity.Publisher,
entity.Publication_date,
entity.Location,
entity.Description,
entity.Copies.Count()
);
}
public ICollection<BookModel> getAll()
{
List<BookModel> result = new();
foreach (var item in _bookRepository.FindAll())
{
result.Add(getModel(item));
};
return result;
}
public BookModel getById(int id)
{
return getModel(_bookRepository.FindByIdOrThrow(id));
}
}
9 changes: 9 additions & 0 deletions web-library/Book/DataProvider/IBookDataProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using web_library.Book.Model;

namespace web_library.Book.DataProvider;

public interface IBookDataProvider
{
ICollection<BookModel> getAll();
BookModel getById(int id);
}
27 changes: 27 additions & 0 deletions web-library/Book/Model/BookModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace web_library.Book.Model;

public class BookModel
{
public int BookId { get; set; }
public string ISBN { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string Publisher { get; set; }
public DateOnly Publication_date { get; set; }
public string Location { get; set; }
public string Description { get; set; }
public int NumberOfCopies { get; set; }

public BookModel(int bookId, string iSBN, string title, string author, string publisher, DateOnly publication_date, string location, string description, int numberOfCopies)
{
BookId = bookId;
ISBN = iSBN;
Title = title;
Author = author;
Publisher = publisher;
Publication_date = publication_date;
Location = location;
Description = description;
NumberOfCopies = numberOfCopies;
}
}
7 changes: 4 additions & 3 deletions web-library/Book/Repository/BookCopyRepository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using web_library.Book.Entity;
using web_library.SharedExceptions;

namespace web_library.Book.Repository
{
Expand All @@ -18,14 +19,14 @@ public void Add(BookCopy entity)
_context.SaveChanges();
}

public IEnumerable<BookCopy> GetAll()
public IEnumerable<BookCopy> FindAll()
{
throw new NotImplementedException();
}

public BookCopy GetByIdOrThrow(int id)
public BookCopy FindByIdOrThrow(int id)
{
throw new NotImplementedException();
return _context.BooksCopy.Find(id) ?? throw new NotFoundException("Book copy" + id + " not found in repository");
}

public void Remove(BookCopy entity)
Expand Down
63 changes: 31 additions & 32 deletions web-library/Book/Repository/BookRepository.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
using System;
using System.Collections.Generic;
using web_library.Book.DataProvider;
using Microsoft.EntityFrameworkCore;
using web_library.SharedExceptions;

namespace web_library.Book.Repository
namespace web_library.Book.Repository;
using Entity;

public class BookRepository : IBookRepository
{
public class BookRepository : IBookRepository
private readonly DataContext _context;
public BookRepository(DataContext context)
{
private readonly DataContext _context;
public BookRepository(DataContext context)
{
_context = context;
}
_context = context;
}

public void Add(Entity.Book entity)
{
_context.Books.Add(entity);
_context.SaveChanges();
}
public void Add(Book entity)
{
_context.Books.Add(entity);
_context.SaveChanges();
}

public IEnumerable<Entity.Book> GetAll()
{
throw new NotImplementedException();
}
public IEnumerable<Book> FindAll()
{
return _context.Books.Include(b => b.Copies).ToList();
}

public Entity.Book GetByIdOrThrow(int id)
{
throw new NotImplementedException();
}
public Book FindByIdOrThrow(int id)
{
return _context.Books.Find(id) ?? throw new NotFoundException("Book " + id + " not found in repository");
}

public void Remove(Entity.Book entity)
{
throw new NotImplementedException();
}
public void Remove(Book entity)
{
throw new NotImplementedException();
}

public void Update(Entity.Book entity)
{
_context.Books.Update(entity);
_context.SaveChanges();
}
public void Update(Book entity)
{
_context.Books.Update(entity);
_context.SaveChanges();
}
}
7 changes: 3 additions & 4 deletions web-library/Book/Repository/IBookCopyRepository.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using web_library.Book.Entity;

namespace web_library.Book.Repository
namespace web_library.Book.Repository;

public interface IBookCopyRepository : IGenericRepository<BookCopy>
{
public interface IBookCopyRepository : IGenericRepository<BookCopy>
{
}
}
11 changes: 5 additions & 6 deletions web-library/Book/Repository/IBookRepository.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace web_library.Book.DataProvider
namespace web_library.Book.Repository;

using Entity;
public interface IBookRepository : IGenericRepository<Book>
{
using Entity;
public interface IBookRepository : IGenericRepository<Book>
{
public void Update(Book book);
}
public void Update(Book book);
}
12 changes: 12 additions & 0 deletions web-library/Book/Request/AssigneGenreToBookRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace web_library.Book.Request
{
public class AssigneGenreToBookRequest
{

public ICollection<int> genres_ids { get; set; }
[JsonIgnore]
public int book_id { get; set; }
}
}
44 changes: 33 additions & 11 deletions web-library/Book/Request/CreateBookRequest.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
using System;

namespace web_library.Book.Request
namespace web_library.Book.Request
{
public class CreateBookRequest
{
public string ISBN { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string Publisher { get; set; }
public DateOnly Publication_date { get; set; }
public string Location { get; set; }
public string Description { get; set; }
public int NumberOfCopies { get; set; }
public string isbn { get; set; }
public string title { get; set; }
public string author { get; set; }
public string publisher { get; set; }
public DateOnly publication_date { get; set; }
public string location { get; set; }
public string description { get; set; }
public int numberOfCopies { get; set; }

public CreateBookRequest() { }
public CreateBookRequest(string type)
{
List<long> isbnList = new() { 9780439023528, 9780747532743, 9780451524935, 9780618260300, 9780141439600 };
List<string> titleList = new() { "The Hunger Games", "Harry Potter and the Philosopher's Stone", "1984", "The Hobbit", "Pride and Prejudice" };
List<string> authorList = new() { "Suzanne Collins", "J.K. Rowling", "George Orwell", "J.R.R. Tolkien", "Jane Austen" };
List<string> publisherList = new() { "Scholastic Press", "Bloomsbury", "Harcourt Brace Jovanovich", "Houghton Mifflin", "Penguin Books" };
List<string> publication_dateList = new() { "September 14, 2008", "June 26, 1997", "June 8, 1949", "September 21, 1937", "January 28, 1813" };
List<string> locationList = new() { "United States", "United Kingdom", "United Kingdom", "United Kingdom", "United Kingdom" };
List<string> descriptionList = new() { "A dystopian novel set in a post-apocalyptic society in the country of Panem.", "The first novel in the Harry Potter series and Rowling's debut novel.", "A dystopian social science fiction novel and a cautionary tale of a totalitarian regime.", "A children's fantasy novel and prelude to the Lord of the Rings series.", "A romantic novel of manners depicting the society of early 19th-century England." };
List<int> numberOfCopiesList = new() { 5, 10, 15, 20, 25 };

Random r = new Random();
int bookNumber = r.Next(0, 5);
isbn = isbnList[bookNumber].ToString();
title = titleList[bookNumber];
author = authorList[bookNumber];
publisher = publisherList[bookNumber];
publication_date = DateOnly.Parse(publication_dateList[r.Next(0, 5)]);
location = locationList[bookNumber];
description = descriptionList[bookNumber];
numberOfCopies = numberOfCopiesList[bookNumber];
}
}
}
Loading