diff --git a/BookStore.API/Controllers/CustomersController.cs b/BookStore.API/Controllers/CustomersController.cs new file mode 100644 index 0000000..7086203 --- /dev/null +++ b/BookStore.API/Controllers/CustomersController.cs @@ -0,0 +1,110 @@ +using BookStore.API.DTOs; +using BookStore.DAL; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; + +namespace BookStore.API.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class CustomersController : ControllerBase + { + private readonly BookStoreContext _context; + + public CustomersController(BookStoreContext context) + { + _context = context; + } + + // GET: api/customers + [HttpGet] + public async Task GetAll() + { + var customers = await _context.Customers + .Include(c => c.Orders) + .Select(c => new CustomerDto + { + Id = c.Id, + Name = c.Name, + Email = c.Email, + + OrderIds = c.Orders.Select(o => o.Id).ToList() + }) + .ToListAsync(); + + return Ok(customers); + } + + // GET: api/customers/5 + [HttpGet("{id}")] + public async Task GetById(int id) + { + var customer = await _context.Customers + .Include(c => c.Orders) + .Where(c => c.Id == id) + .Select(c => new CustomerDto + { + Id = c.Id, + Name = c.Name, + Email = c.Email, + + OrderIds = c.Orders.Select(o => o.Id).ToList() + }) + .FirstOrDefaultAsync(); + + if (customer == null) + return NotFound(); + + return Ok(customer); + } + + // POST: api/customers + [HttpPost] + public async Task Create(CreateCustomerDto dto) + { + var customer = new Customer + { + Name = dto.Name, + Email = dto.Email + }; + + _context.Customers.Add(customer); + await _context.SaveChangesAsync(); + + return CreatedAtAction(nameof(GetById), new { id = customer.Id }, null); + } + + // PUT: api/customers/5 + [HttpPut("{id}")] + public async Task Update(int id, CreateCustomerDto dto) + { + var customer = await _context.Customers + .FirstOrDefaultAsync(c => c.Id == id); + + if (customer == null) + return NotFound(); + + customer.Name = dto.Name; + customer.Email = dto.Email; + + await _context.SaveChangesAsync(); + + return NoContent(); + } + + // DELETE: api/customers/5 + [HttpDelete("{id}")] + public async Task Delete(int id) + { + var customer = await _context.Customers.FindAsync(id); + + if (customer == null) + return NotFound(); + + _context.Customers.Remove(customer); + await _context.SaveChangesAsync(); + + return NoContent(); + } + } +} diff --git a/BookStore.API/DTOs/CustomerDto.cs b/BookStore.API/DTOs/CustomerDto.cs new file mode 100644 index 0000000..43bda0e --- /dev/null +++ b/BookStore.API/DTOs/CustomerDto.cs @@ -0,0 +1,17 @@ +namespace BookStore.API.DTOs +{ + public class CustomerDto + { + public int Id { get; set; } + public string? Name { get; set; } + public string? Email { get; set; } + + public List OrderIds { get; set; } = new(); + } + + public class CreateCustomerDto + { + public string? Name { get; set; } + public string? Email { get; set; } + } +} diff --git a/BookStoreDb.sql b/BookStoreDb.sql index acc38db..5c82e6e 100644 --- a/BookStoreDb.sql +++ b/BookStoreDb.sql @@ -7,3 +7,23 @@ VALUES ('Programming'); INSERT INTO Books (Title, ISBN, Price, Stock, CategoryId, PublisherId) VALUES ('Clean Code', '9780132350884', 29.99, 10, 1, 1); +INSERT INTO Customers (Name, Email) +VALUES +('Ali Khan', 'ali.khan@example.com'), +('Sara Ahmed', 'sara.ahmed@example.com'), +('John Smith', 'john.smith@example.com'), +('Emily Johnson', 'emily.johnson@example.com'), +('Omar Farooq', 'omar.farooq@example.com'), +('Ayesha Malik', 'ayesha.malik@example.com'), +('Hassan Raza', 'hassan.raza@example.com'), +('Fatima Noor', 'fatima.noor@example.com'), +('David Brown', 'david.brown@example.com'), +('Michael Scott', 'michael.scott@dundermifflin.com'), +('Pam Beesly', 'pam.beesly@dundermifflin.com'), +('Jim Halpert', 'jim.halpert@dundermifflin.com'), +('Dwight Schrute', 'dwight.schrute@dundermifflin.com'), +('Zain Ali', 'zain.ali@example.com'), +('Noor Fatima', 'noor.fatima@example.com'); + + +