-
Notifications
You must be signed in to change notification settings - Fork 0
T3(customer): add full CRUD controller, service impl, DTO, and JWT auth #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devin-ai-integration
wants to merge
1
commit into
devin/customer-service-t2
Choose a base branch
from
devin/customer-service-t3
base: devin/customer-service-t2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
93 changes: 86 additions & 7 deletions
93
src/Services/Customer/Customer.API/Controllers/CustomerController.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,108 @@ | ||
| using Microsoft.AspNetCore.Authorization; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Customer.API.DTOs; | ||
| using Customer.Domain.Interfaces; | ||
| using Customer.Domain.Enums; | ||
| using Entities = Customer.Domain.Entities; | ||
|
|
||
| namespace Customer.API.Controllers; | ||
|
|
||
| [ApiController] | ||
| [Route("api/[controller]")] | ||
| [Authorize] | ||
| public class CustomerController : ControllerBase | ||
| { | ||
| private readonly ICustomerService _customerService; | ||
| private readonly ILogger<CustomerController> _logger; | ||
|
|
||
| public CustomerController(ILogger<CustomerController> logger) | ||
| public CustomerController(ICustomerService customerService, ILogger<CustomerController> logger) | ||
| { | ||
| _customerService = customerService; | ||
| _logger = logger; | ||
| } | ||
|
|
||
| [HttpGet] | ||
| public IActionResult GetAll() | ||
| [ProducesResponseType(typeof(IEnumerable<CustomerDto>), StatusCodes.Status200OK)] | ||
| [ProducesResponseType(StatusCodes.Status401Unauthorized)] | ||
| public async Task<IActionResult> GetAll() | ||
| { | ||
| // TODO: Implement — migrate logic from monolith's CustomerController | ||
| return Ok(new { service = "Customer", status = "scaffold" }); | ||
| var customers = await _customerService.GetAllAsync(); | ||
| return Ok(customers.Select(MapToDto)); | ||
| } | ||
|
|
||
| [HttpGet("{id}")] | ||
| public IActionResult GetById(int id) | ||
| [ProducesResponseType(typeof(CustomerDto), StatusCodes.Status200OK)] | ||
| [ProducesResponseType(StatusCodes.Status404NotFound)] | ||
| [ProducesResponseType(StatusCodes.Status401Unauthorized)] | ||
| public async Task<IActionResult> GetById(int id) | ||
| { | ||
| // TODO: Implement — migrate logic from monolith | ||
| return Ok(new { service = "Customer", id }); | ||
| var customer = await _customerService.GetByIdAsync(id); | ||
| if (customer is null) return NotFound(); | ||
| return Ok(MapToDto(customer)); | ||
| } | ||
|
|
||
| [HttpPost] | ||
| [ProducesResponseType(typeof(CustomerDto), StatusCodes.Status201Created)] | ||
| [ProducesResponseType(StatusCodes.Status400BadRequest)] | ||
| [ProducesResponseType(StatusCodes.Status401Unauthorized)] | ||
| public async Task<IActionResult> Create([FromBody] CustomerDto dto) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(dto.Name) || string.IsNullOrWhiteSpace(dto.Email)) | ||
| return BadRequest("Name and Email are required."); | ||
|
|
||
| var customer = new Entities.Customer | ||
| { | ||
| Name = dto.Name, | ||
| Email = dto.Email, | ||
| PhoneNumber = dto.PhoneNumber, | ||
| Address = dto.Address, | ||
| City = dto.City, | ||
| Gender = Enum.TryParse<Gender>(dto.Gender, true, out var g) ? g : Gender.None | ||
| }; | ||
|
|
||
| var created = await _customerService.CreateAsync(customer); | ||
| return CreatedAtAction(nameof(GetById), new { id = created.Id }, MapToDto(created)); | ||
| } | ||
|
|
||
| [HttpPut("{id}")] | ||
| [ProducesResponseType(StatusCodes.Status204NoContent)] | ||
| [ProducesResponseType(StatusCodes.Status404NotFound)] | ||
| [ProducesResponseType(StatusCodes.Status401Unauthorized)] | ||
| public async Task<IActionResult> Update(int id, [FromBody] CustomerDto dto) | ||
| { | ||
| var existing = await _customerService.GetByIdAsync(id); | ||
| if (existing is null) return NotFound(); | ||
|
|
||
| existing.Name = dto.Name ?? existing.Name; | ||
| existing.Email = dto.Email ?? existing.Email; | ||
| existing.PhoneNumber = dto.PhoneNumber; | ||
| existing.Address = dto.Address; | ||
| existing.City = dto.City; | ||
| existing.Gender = Enum.TryParse<Gender>(dto.Gender, true, out var g) ? g : existing.Gender; | ||
|
|
||
| await _customerService.UpdateAsync(existing); | ||
| return NoContent(); | ||
| } | ||
|
|
||
| [HttpDelete("{id}")] | ||
| [ProducesResponseType(StatusCodes.Status204NoContent)] | ||
| [ProducesResponseType(StatusCodes.Status404NotFound)] | ||
| [ProducesResponseType(StatusCodes.Status401Unauthorized)] | ||
| public async Task<IActionResult> Delete(int id) | ||
| { | ||
| var deleted = await _customerService.DeleteAsync(id); | ||
| if (!deleted) return NotFound(); | ||
| return NoContent(); | ||
| } | ||
|
|
||
| private static CustomerDto MapToDto(Entities.Customer c) => new() | ||
| { | ||
| Id = c.Id, | ||
| Name = c.Name, | ||
| Email = c.Email, | ||
| PhoneNumber = c.PhoneNumber, | ||
| Address = c.Address, | ||
| City = c.City, | ||
| Gender = c.Gender.ToString() | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| namespace Customer.API.DTOs; | ||
|
|
||
| public class CustomerDto | ||
| { | ||
| public int Id { get; set; } | ||
| public string? Name { get; set; } | ||
| public string? Email { get; set; } | ||
| public string? PhoneNumber { get; set; } | ||
| public string? Address { get; set; } | ||
| public string? City { get; set; } | ||
| public string? Gender { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
51 changes: 51 additions & 0 deletions
51
src/Services/Customer/Customer.Infrastructure/Services/CustomerService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Customer.Domain.Interfaces; | ||
| using Customer.Infrastructure.Data; | ||
| using Entities = Customer.Domain.Entities; | ||
|
|
||
| namespace Customer.Infrastructure.Services; | ||
|
|
||
| public class CustomerService : ICustomerService | ||
| { | ||
| private readonly CustomerDbContext _context; | ||
|
|
||
| public CustomerService(CustomerDbContext context) | ||
| { | ||
| _context = context; | ||
| } | ||
|
|
||
| public async Task<IEnumerable<Entities.Customer>> GetAllAsync() | ||
| { | ||
| return await _context.Customers.OrderBy(c => c.Name).ToListAsync(); | ||
| } | ||
|
|
||
| public async Task<Entities.Customer?> GetByIdAsync(int id) | ||
| { | ||
| return await _context.Customers.FindAsync(id); | ||
| } | ||
|
|
||
| public async Task<Entities.Customer> CreateAsync(Entities.Customer customer) | ||
| { | ||
| customer.CreatedDate = DateTime.UtcNow; | ||
| customer.UpdatedDate = DateTime.UtcNow; | ||
| _context.Customers.Add(customer); | ||
| await _context.SaveChangesAsync(); | ||
| return customer; | ||
| } | ||
|
|
||
| public async Task UpdateAsync(Entities.Customer customer) | ||
| { | ||
| customer.UpdatedDate = DateTime.UtcNow; | ||
| _context.Customers.Update(customer); | ||
| await _context.SaveChangesAsync(); | ||
| } | ||
|
|
||
| public async Task<bool> DeleteAsync(int id) | ||
| { | ||
| var customer = await _context.Customers.FindAsync(id); | ||
| if (customer == null) return false; | ||
| _context.Customers.Remove(customer); | ||
| await _context.SaveChangesAsync(); | ||
| return true; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 Updating a customer silently erases phone number, address, and city when those fields are omitted
Optional customer fields are overwritten with null (
dto.PhoneNumber/dto.Address/dto.Cityassigned directly atCustomerController.cs:78-80) instead of being preserved like name and email are, so any update request that omits those fields wipes out the stored values.Impact: A user updating only their name or email loses their phone number, address, and city data.
Inconsistent null-coalescing between required and optional fields in the Update action
In the
Updatemethod atsrc/Services/Customer/Customer.API/Controllers/CustomerController.cs:76-81,NameandEmailuse the null-coalescing operator (??) so a null DTO value preserves the existing database value:But the three optional fields are assigned directly without the same guard:
When a PUT request omits
PhoneNumber,Address, orCity(they deserialize asnullfrom theCustomerDto), the existing stored values are overwritten withnull. This is an inconsistency: the code intends partial-update semantics (as evidenced by the??pattern on the other fields and onGenderat line 81), but three fields use full-replace semantics instead.Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — the inconsistency is real. The direct assignment for
PhoneNumber/Address/Citywas implemented per the task spec, but this does create different semantics vs the??-guardedName/Email/Genderfields. Escalating to the author to confirm whether partial-update semantics (null-coalescing) should apply to all fields.