File size: 5,152 Bytes
87c9973
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using Database.Models;
using Microsoft.EntityFrameworkCore;

namespace LibraryManagement.Backend.Features.Books
{
    public interface IBookService
    {
        Task<IEnumerable<BookDto>> GetAllBooksAsync();
        Task<BookDto?> GetBookByIdAsync(int id);
        Task<BookDto> CreateBookAsync(BookCreateRequest request);
        Task<BookDto?> UpdateBookAsync(int id, BookUpdateRequest request);
        Task<bool> DeleteBookAsync(int id);
    }

    public class BookService : IBookService
    {
        private readonly LibraryManagementContext _context;

        public BookService(LibraryManagementContext context)
        {
            _context = context;
        }

        public async Task<IEnumerable<BookDto>> GetAllBooksAsync()
        {
            return await _context.Books
                .Where(b => b.IsActive)
                .Select(b => MapToDto(b))
                .ToListAsync();
        }

        public async Task<BookDto?> GetBookByIdAsync(int id)
        {
            var book = await _context.Books.FindAsync(id);
            if (book == null || !book.IsActive) return null;
            return MapToDto(book);
        }

        public async Task<BookDto> CreateBookAsync(BookCreateRequest request)
        {
            if (await _context.Books.AnyAsync(b => b.Isbn == request.Isbn))
            {
                throw new Exception("A book with this ISBN already exists.");
            }

            var book = new Book
            {
                Title = request.Title,
                Isbn = request.Isbn,
                Author = request.Author,
                Description = request.Description,
                CoverUrl = request.CoverUrl,
                TotalCopies = request.TotalCopies,
                AvailableCopies = request.TotalCopies,
                Status = request.TotalCopies > 0 ? "Available" : "Out Of Stock",
                IsActive = true,
                CreatedAt = DateTime.UtcNow
            };

            _context.Books.Add(book);
            await _context.SaveChangesAsync();

            return MapToDto(book);
        }

        public async Task<BookDto?> UpdateBookAsync(int id, BookUpdateRequest request)
        {
            var book = await _context.Books.FindAsync(id);
            if (book == null || !book.IsActive) return null;

            book.Title = request.Title;
            book.Author = request.Author;
            book.Description = request.Description;
            book.CoverUrl = request.CoverUrl;
            
            // Basic logic to sync availability when total copies change
            int difference = request.TotalCopies - book.TotalCopies;
            book.TotalCopies = request.TotalCopies;
            book.AvailableCopies += difference;

            if (book.AvailableCopies < 0) book.AvailableCopies = 0;
            book.Status = book.AvailableCopies > 0 ? "Available" : "Out Of Stock";
            
            book.UpdatedAt = DateTime.UtcNow;

            await _context.SaveChangesAsync();
            return MapToDto(book);
        }

        public async Task<bool> DeleteBookAsync(int id)
        {
            var book = await _context.Books.FindAsync(id);
            if (book == null) return false;

            book.IsActive = false;
            book.UpdatedAt = DateTime.UtcNow;

            await _context.SaveChangesAsync();
            return true;
        }

        private static BookDto MapToDto(Book book)
        {
            return new BookDto
            {
                Id = book.Id,
                Title = book.Title,
                Isbn = book.Isbn,
                Author = book.Author,
                Status = book.Status,
                Description = book.Description,
                CoverUrl = book.CoverUrl,
                TotalCopies = book.TotalCopies,
                AvailableCopies = book.AvailableCopies,
                CreatedAt = book.CreatedAt
            };
        }
    }

    public class BookDto
    {
        public int Id { get; set; }
        public string Title { get; set; } = string.Empty;
        public string Isbn { get; set; } = string.Empty;
        public string Author { get; set; } = string.Empty;
        public string Status { get; set; } = string.Empty;
        public string? Description { get; set; }
        public string? CoverUrl { get; set; }
        public int TotalCopies { get; set; }
        public int AvailableCopies { get; set; }
        public DateTime CreatedAt { get; set; }
    }

    public class BookCreateRequest
    {
        public string Title { get; set; } = string.Empty;
        public string Isbn { get; set; } = string.Empty;
        public string Author { get; set; } = string.Empty;
        public string? Description { get; set; }
        public string? CoverUrl { get; set; }
        public int TotalCopies { get; set; }
    }

    public class BookUpdateRequest
    {
        public string Title { get; set; } = string.Empty;
        public string Author { get; set; } = string.Empty;
        public string? Description { get; set; }
        public string? CoverUrl { get; set; }
        public int TotalCopies { get; set; }
    }
}