GOD Methods or reusing small?

Discussão em 'Angular' iniciado por Aleksandro, Dezembro 5, 2024.

  1. Aleksandro

    Aleksandro Guest

    public class Book
    {
    [Key]
    public int BookId { get; set; }
    public string Title { get; set; } = string.Empty;
    public string Description { get; set; } = string.Empty;
    public string Language { get; set; } = string.Empty;
    public string ISBN { get; set; } = string.Empty;
    public string Genre { get; set; } = string.Empty;
    public string Type { get; set; } = string.Empty;
    public int PublicationYear { get; set; }
    [Column(TypeName = "decimal(18,2)")]
    public decimal Price { get; set; }
    public int Quantity { get; set; }
    public string ImagePath { get; set; } = string.Empty;

    public ICollection<Book_Author> BookAuthors { get; set; } = new List<Book_Author>
    ();

    [ForeignKey("Publisher")]
    public int PublisherId { get; set; }
    public Publisher? Publisher { get; set; }
    }

    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Text.Json.Serialization;

    namespace API.Data.Entities
    {
    public class Author
    {
    [Key]
    public int AuthorId { get; set; }
    public string FirstName { get; set; } = string.Empty;
    public string LastName { get; set; } = string.Empty;

    [NotMapped]
    public string FullName => $"{FirstName} {LastName}";
    [JsonIgnore]
    public ICollection<Book_Author> Book_Authors { get; set; } = new List<Book_Author>
    ();
    }
    }

    using System.ComponentModel.DataAnnotations;
    using System.Text.Json.Serialization;

    namespace API.Data.Entities
    {
    public class Publisher
    {
    [Key]
    public int PublisherId { get; set; }
    public string Name { get; set; } = string.Empty;

    [JsonIgnore]
    public ICollection<Book> Books { get; set; } = new List<Book>();
    }
    }


    I have entities and book service for admin.(CRUD Methods).Same logic goes for a Publisher and Author.

    using API.Data.Entities;
    using API.Interfaces;
    using Microsoft.AspNetCore.Http.HttpResults;

    namespace API.Services
    {
    public class BooksAdminService : IBooksAdminService
    {
    private readonly IRepository<Book> _bookRepository;
    public BooksAdminService(IRepository<Book> bookRepository)
    {
    _bookRepository = bookRepository;
    }
    public async Task<Book> CreateBook(Book book)
    {
    if (book.Title == string.Empty && book.Title == null) throw new
    Exception("Title must be provided!");
    var newBook = await _bookRepository.AddAsync(book);

    return newBook;
    }

    public async Task DeleteBook(Book book)
    {
    await _bookRepository.DeleteAsync(book);
    }

    public async Task<Book> GetBookById(int id)
    {
    var book = await _bookRepository.GetByIdAsync(id);
    if(book == null)
    {
    throw new Exception("Book not found");
    }

    return book;
    }

    public async Task<IEnumerable<Book>> GetBooksAsync()
    {
    var books = await _bookRepository.GetAllAsync();
    if (books == null) throw new Exception("There is no books!");

    return books;
    }

    public async Task UpdateBook(Book book)
    {
    await _bookRepository.UpdateAsync(book);
    }
    }
    }


    I want admin to create a book with all related enitites at the same time. Admin can choose existing author and publisher or create a new one's. What is the right approach:

    1. Creating god methods using all services?
    2. Create something else?

    Frontend is gonna be on Angular.

    Example controller goes like this:
    using API.Data.Entities;
    using API.Dto.Request;
    using API.Interfaces;
    using Microsoft.AspNetCore.Mvc;

    namespace API.Controllers
    {
    public class BooksAdminController : BaseContoller
    {
    private readonly IBooksAdminService _booksAdminService;

    public BooksAdminController(IBooksAdminService booksAdminService)
    {
    _booksAdminService = booksAdminService;
    }

    [HttpGet]
    public async Task<ActionResult<IEnumerable<Book>>> GetAllBooks()
    {
    var books = await _booksAdminService.GetBooksAsync();

    return Ok(books);
    }


    [HttpGet("{id}")]
    public async Task<ActionResult<Book>> GetBook(int id)
    {
    var book = await _booksAdminService.GetBookById(id);

    if(book == null) return NotFound("Book not found");

    return Ok(book);
    }

    [HttpPost]
    public async Task<ActionResult<Book>> CreateBook([FromBody] BookCreateDto
    bookDto)
    {
    var book = new Book()
    {
    Title = bookDto.Title,
    Description = bookDto.Description,
    Language = bookDto.Language,
    ISBN = bookDto.ISBN,
    Genre = bookDto.Genre,
    Type = bookDto.Type,
    PublicationYear = bookDto.PublicationYear,
    Price = bookDto.Price,
    Quantity = bookDto.Quantity,
    ImagePath = bookDto.ImagePath,
    PublisherId = bookDto.PublisherId,
    };

    await _booksAdminService.CreateBook(book);

    return Ok(book);
    }

    [HttpPut("{id}")]
    public async Task<ActionResult> UpdateBook(int id, [FromBody] BookCreateDto
    bookDto)
    {
    var existingBook = await _booksAdminService.GetBookById(id);

    if(existingBook == null) return NotFound("No book found");

    existingBook.Title = bookDto.Title;
    existingBook.Description = bookDto.Description;
    existingBook.Language = bookDto.Language;
    existingBook.ISBN = bookDto.ISBN;
    existingBook.Genre = bookDto.Genre;
    existingBook.Type = bookDto.Type;
    existingBook.PublicationYear = bookDto.PublicationYear;
    existingBook.Price = bookDto.Price;
    existingBook.Quantity = bookDto.Quantity;
    existingBook.ImagePath = bookDto.ImagePath;
    existingBook.PublisherId = bookDto.PublisherId;

    await _booksAdminService.UpdateBook(existingBook);
    return NoContent();
    }

    [HttpDelete("{id}")]
    public async Task<IActionResult> DeleteBook(int id)
    {
    var book = await _booksAdminService.GetBookById(id);

    if (book == null) return NotFound("Book is not found");

    await _booksAdminService.DeleteBook(book);

    return NoContent();
    }
    }
    }

    Continue reading...

Compartilhe esta Página