/
egopen
/
Lab1
Обзор
Документация
Войти
/
egopen
/
Lab1
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Lab4/library/Services/BookManagingService.cs
154 строки
5 KB
Egopen
lab 4
01 ноя 2025, 18:03
01 ноя 2025, 18:03
78d017c
Код
Авторство
О чём код?
using library.DB; using library.DB.Models; using library.DTO; using library.Errors; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Caching.Memory; using static Microsoft.EntityFrameworkCore.DbLoggerCategory; namespace library.Services { public class BookManagingService { private readonly LibraryDBContext _db; private readonly IMemoryCache _memoryCache; public BookManagingService(LibraryDBContext db, IMemoryCache memoryCache) { _db = db; _memoryCache = memoryCache; } public async Task<Book> GetBook(Guid bookId) { var book = await _db.Books.FirstOrDefaultAsync(b => b.Id == bookId); if (book == null) { throw new NotFoundException("Book not found"); } return book; } public async Task<List<Book>> GetBooks(int page, int pageSize, bool withAuthor) { var query = _db.Books.AsQueryable(); if (withAuthor) { query = query.Include(b => b.Author); } var books = await query .Skip((page - 1) * pageSize) .Take(pageSize) .ToListAsync(); return books; } public async Task<List<Book>> GetBooksByAuthor(Guid authorId, int page) { var books = await _db.Books.Where(b=> b.AuthorId == authorId).Skip((page - 1) * 20).Take(20).ToListAsync(); return books; } public async Task<Book> AddBook(Guid authorId, string title, DateTime created) { var idempotencyKey = $"add_book_{authorId}_{title}_{created:yyyyMMdd}"; if (_memoryCache.TryGetValue(idempotencyKey, out Book cachedBook)) { return cachedBook; } Book book = new Book(); book.AuthorId = authorId; book.Title = title; book.CreationDate = created; book.Id = Guid.NewGuid(); await _db.Books.AddAsync(book); await _db.SaveChangesAsync(); _memoryCache.Set(idempotencyKey, book, TimeSpan.FromMinutes(5)); return book; } public async Task<Author> GetAuthor(Guid authorId) { var author = await _db.Authors.FirstOrDefaultAsync(a => a.Id == authorId); if(author == null) { throw new NotFoundException("author not found"); } return author; } public async Task<Author> AddAuthor(string name, DateTime birthDate) { var idempotencyKey = $"add_author_{name}_{birthDate:yyyyMMdd}"; if (_memoryCache.TryGetValue(idempotencyKey, out Author cachedAuthor)) { return cachedAuthor; } Author author = new Author(); author.Id = Guid.NewGuid(); author.Name = name; author.BirthDate = birthDate; await _db.Authors.AddAsync(author); await _db.SaveChangesAsync(); _memoryCache.Set(idempotencyKey, author, TimeSpan.FromMinutes(5)); return author; } public async Task DeleteAuthor(Guid authorId) { var author = await _db.Authors.FirstOrDefaultAsync(a=> a.Id == authorId); _db.Authors.Remove(author); await _db.SaveChangesAsync(); } public async Task DeleteBook(Guid bookId) { var book = await _db.Books.FirstOrDefaultAsync(b => b.Id == bookId); if (book == null) { throw new NotFoundException("Book not found"); } _db.Books.Remove(book); await _db.SaveChangesAsync(); } public async Task<Book> UpdateBook(UpdateBookDTO dto) { var idempotencyKey = $"update_book_{dto.Id}_{dto.Title}_{dto.AuthorId}_{dto.CreationDate?.ToString("yyyyMMdd")}"; if (_memoryCache.TryGetValue(idempotencyKey, out Book cachedBook)) { return cachedBook; } var book = await _db.Books.FirstOrDefaultAsync(u => u.Id == dto.Id); if (book == null) { throw new NotFoundException("Book not found"); } if (dto.CreationDate != null) { book.CreationDate = dto.CreationDate.Value; } if (dto.Title != null) { if (dto.Title.Length < 0) { throw new InvalidDataException("Wrong title format"); } book.Title = dto.Title; } if (dto.AuthorId != null) { if (!await _db.Authors.AnyAsync(a => a.Id == dto.AuthorId)) { throw new NotFoundException("AuthorNotFound"); } book.AuthorId = dto.AuthorId.Value; } await _db.SaveChangesAsync(); _memoryCache.Set(idempotencyKey, book, TimeSpan.FromMinutes(5)); return book; } } }