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
110 changes: 110 additions & 0 deletions BookStore.API/Controllers/CustomersController.cs
Original file line number Diff line number Diff line change
@@ -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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> Delete(int id)
{
var customer = await _context.Customers.FindAsync(id);

if (customer == null)
return NotFound();

_context.Customers.Remove(customer);
await _context.SaveChangesAsync();

return NoContent();
}
}
}
17 changes: 17 additions & 0 deletions BookStore.API/DTOs/CustomerDto.cs
Original file line number Diff line number Diff line change
@@ -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<int> OrderIds { get; set; } = new();
}

public class CreateCustomerDto
{
public string? Name { get; set; }
public string? Email { get; set; }
}
}
20 changes: 20 additions & 0 deletions BookStoreDb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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');