#Refactor WondersController: Apply Clean Code principles (Uncle Bob style)
هذا الالتزام موجود في:
@@ -6,6 +6,9 @@ using WondersAPI.Models;
|
||||
|
||||
namespace Ghaymah.WondersAPI.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// API controller to manage Wonders (CRUD + Random).
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class WondersController : ControllerBase
|
||||
@@ -19,87 +22,155 @@ namespace Ghaymah.WondersAPI.Controllers
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
// -------------------- GET ALL --------------------
|
||||
/// <summary>
|
||||
/// Get all wonders.
|
||||
/// </summary>
|
||||
/// <returns>List of all wonders.</returns>
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetAll()
|
||||
[ProducesResponseType(typeof(IEnumerable<Wonder>), 200)]
|
||||
public async Task<IActionResult> GetAllWonders()
|
||||
{
|
||||
var wonders = await _context.Wonders.ToListAsync();
|
||||
_logger.LogInformation("Fetched {Count} wonders from database", wonders.Count);
|
||||
var wonders = await FetchAllWonders();
|
||||
LogFetchedWonders(wonders.Count);
|
||||
return Ok(wonders);
|
||||
}
|
||||
|
||||
private async Task<List<Wonder>> FetchAllWonders() =>
|
||||
await _context.Wonders.ToListAsync();
|
||||
|
||||
private void LogFetchedWonders(int count) =>
|
||||
_logger.LogInformation("Fetched {Count} wonders from database", count);
|
||||
|
||||
// -------------------- GET BY ID --------------------
|
||||
/// <summary>
|
||||
/// Get a specific wonder by its ID.
|
||||
/// </summary>
|
||||
/// <param name="wonderId">The ID of the wonder.</param>
|
||||
/// <returns>The wonder with the specified ID.</returns>
|
||||
[HttpGet("{id}")]
|
||||
public async Task<IActionResult> GetById(int id)
|
||||
[ProducesResponseType(typeof(Wonder), 200)]
|
||||
[ProducesResponseType(404)]
|
||||
public async Task<IActionResult> GetWonderById(int wonderId)
|
||||
{
|
||||
var wonder = await _context.Wonders.FindAsync(id);
|
||||
if (wonder == null)
|
||||
{
|
||||
_logger.LogWarning("Wonder with ID {Id} not found", id);
|
||||
return NotFound(new { message = $"Wonder with ID {id} not found" });
|
||||
}
|
||||
var wonder = await FindWonderOrNotFound(wonderId);
|
||||
if (wonder is null) return NotFound(new { message = $"Wonder with ID {wonderId} not found" });
|
||||
|
||||
return Ok(wonder);
|
||||
}
|
||||
|
||||
private async Task<Wonder?> FindWonderOrNotFound(int wonderId) =>
|
||||
await _context.Wonders.FindAsync(wonderId);
|
||||
|
||||
// -------------------- CREATE --------------------
|
||||
/// <summary>
|
||||
/// Create a new wonder.
|
||||
/// </summary>
|
||||
/// <param name="newWonder">The wonder to create.</param>
|
||||
/// <returns>The created wonder.</returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Create(Wonder wonder)
|
||||
[ProducesResponseType(typeof(Wonder), 201)]
|
||||
public async Task<IActionResult> CreateWonder([FromBody] Wonder newWonder) =>
|
||||
await SaveNewWonder(newWonder);
|
||||
|
||||
private async Task<IActionResult> SaveNewWonder(Wonder wonder)
|
||||
{
|
||||
_context.Wonders.Add(wonder);
|
||||
await _context.SaveChangesAsync();
|
||||
_logger.LogInformation("Created new wonder: {Name}", wonder.Name);
|
||||
return CreatedAtAction(nameof(GetById), new { id = wonder.Id }, wonder);
|
||||
LogCreatedWonder(wonder.Name);
|
||||
return CreatedAtAction(nameof(GetWonderById), new { wonderId = wonder.Id }, wonder);
|
||||
}
|
||||
|
||||
|
||||
private void LogCreatedWonder(string name) =>
|
||||
_logger.LogInformation("Created new wonder: {Name}", name);
|
||||
|
||||
// -------------------- UPDATE --------------------
|
||||
/// <summary>
|
||||
/// Update an existing wonder.
|
||||
/// </summary>
|
||||
/// <param name="wonderId">The ID of the wonder to update.</param>
|
||||
/// <param name="updatedWonder">The updated wonder data.</param>
|
||||
[HttpPut("{id}")]
|
||||
public async Task<IActionResult> Update(int id, Wonder updatedWonder)
|
||||
[ProducesResponseType(204)]
|
||||
[ProducesResponseType(404)]
|
||||
public async Task<IActionResult> UpdateWonder(int wonderId, [FromBody] Wonder updatedWonder)
|
||||
{
|
||||
var existing = await _context.Wonders.FindAsync(id);
|
||||
if (existing == null)
|
||||
{
|
||||
_logger.LogWarning("Update failed: Wonder with ID {Id} not found", id);
|
||||
return NotFound();
|
||||
}
|
||||
var existingWonder = await FindWonderOrNotFound(wonderId);
|
||||
if (existingWonder is null) return NotFound();
|
||||
|
||||
existing.Name = updatedWonder.Name;
|
||||
existing.Country = updatedWonder.Country;
|
||||
existing.Era = updatedWonder.Era;
|
||||
existing.Type = updatedWonder.Type;
|
||||
existing.Description = updatedWonder.Description;
|
||||
existing.DiscoveryYear = updatedWonder.DiscoveryYear;
|
||||
return await SaveUpdatedWonder(existingWonder, updatedWonder);
|
||||
}
|
||||
|
||||
private async Task<IActionResult> SaveUpdatedWonder(Wonder target, Wonder source)
|
||||
{
|
||||
CopyWonderValues(target, source);
|
||||
await _context.SaveChangesAsync();
|
||||
_logger.LogInformation("Updated wonder with ID {Id}", id);
|
||||
LogUpdatedWonder(target.Id);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> Delete(int id)
|
||||
private void CopyWonderValues(Wonder target, Wonder source)
|
||||
{
|
||||
var wonder = await _context.Wonders.FindAsync(id);
|
||||
if (wonder == null)
|
||||
{
|
||||
_logger.LogWarning("Delete failed: Wonder with ID {Id} not found", id);
|
||||
return NotFound();
|
||||
}
|
||||
target.Name = source.Name;
|
||||
target.Country = source.Country;
|
||||
target.Era = source.Era;
|
||||
target.Type = source.Type;
|
||||
target.Description = source.Description;
|
||||
target.DiscoveryYear = source.DiscoveryYear;
|
||||
}
|
||||
|
||||
private void LogUpdatedWonder(int wonderId) =>
|
||||
_logger.LogInformation("Updated wonder with ID {Id}", wonderId);
|
||||
|
||||
// -------------------- DELETE --------------------
|
||||
/// <summary>
|
||||
/// Delete a wonder by ID.
|
||||
/// </summary>
|
||||
/// <param name="wonderId">The ID of the wonder to delete.</param>
|
||||
[HttpDelete("{id}")]
|
||||
[ProducesResponseType(204)]
|
||||
[ProducesResponseType(404)]
|
||||
public async Task<IActionResult> DeleteWonder(int wonderId)
|
||||
{
|
||||
var wonder = await FindWonderOrNotFound(wonderId);
|
||||
if (wonder is null) return NotFound();
|
||||
|
||||
await RemoveWonder(wonder);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
private async Task RemoveWonder(Wonder wonder)
|
||||
{
|
||||
_context.Wonders.Remove(wonder);
|
||||
await _context.SaveChangesAsync();
|
||||
_logger.LogInformation("Deleted wonder with ID {Id}", id);
|
||||
return NoContent();
|
||||
LogDeletedWonder(wonder.Id);
|
||||
}
|
||||
|
||||
private void LogDeletedWonder(int wonderId) =>
|
||||
_logger.LogInformation("Deleted wonder with ID {Id}", wonderId);
|
||||
|
||||
// -------------------- GET RANDOM --------------------
|
||||
/// <summary>
|
||||
/// Get a random wonder.
|
||||
/// </summary>
|
||||
/// <returns>A randomly selected wonder.</returns>
|
||||
[HttpGet("random")]
|
||||
public async Task<IActionResult> GetRandom()
|
||||
[ProducesResponseType(typeof(Wonder), 200)]
|
||||
[ProducesResponseType(404)]
|
||||
public async Task<IActionResult> GetRandomWonder()
|
||||
{
|
||||
var wonders = await _context.Wonders.ToListAsync();
|
||||
if (!wonders.Any())
|
||||
return NotFound();
|
||||
var allWonders = await FetchAllWonders();
|
||||
if (!allWonders.Any()) return NotFound();
|
||||
|
||||
var random = wonders[new Random().Next(wonders.Count)];
|
||||
_logger.LogInformation("Returned random wonder: {Name}", random.Name);
|
||||
return Ok(random);
|
||||
var randomWonder = SelectRandomWonder(allWonders);
|
||||
LogRandomWonder(randomWonder.Name);
|
||||
return Ok(randomWonder);
|
||||
}
|
||||
|
||||
private Wonder SelectRandomWonder(List<Wonder> wonders) =>
|
||||
wonders[new Random().Next(wonders.Count)];
|
||||
|
||||
private void LogRandomWonder(string name) =>
|
||||
_logger.LogInformation("Returned random wonder: {Name}", name);
|
||||
}
|
||||
}
|
||||
|
المرجع في مشكلة جديدة
حظر مستخدم