Refactor pet action management: rename PetAction to PetActionGather, update related models and services, and enhance resource gathering logic.

This commit is contained in:
2025-02-01 13:50:39 -03:00
parent e1c5eed02d
commit d6d3dc9f44
11 changed files with 350 additions and 29 deletions

View File

@@ -9,13 +9,18 @@ namespace pet_companion_api.Controllers
public class PetController : ControllerBase
{
private readonly PetService petService;
private readonly PetClassService petClassService;
private readonly ILogger<PetController> logger;
private Guid userId = Guid.Parse("f5f4b3b3-3b7b-4b7b-8b7b-7b7b7b7b7b7b");
public PetController(ILogger<PetController> logger, PetService petService)
public PetController(
ILogger<PetController> logger,
PetService petService,
PetClassService petClassService)
{
this.logger = logger;
this.petService = petService;
this.petClassService = petClassService;
}
[HttpGet("")]
@@ -31,8 +36,45 @@ namespace pet_companion_api.Controllers
return CreatedAtAction(nameof(GetAllPets), new { id = createdPet.Id }, createdPet);
}
[HttpPut("{petId}/action")]
public IActionResult UpdatePetAction(string petId, [FromBody] PetUpdateActionRequest actionRequest)
/// <summary>
///
/// </summary>
/// <param name="petId"></param>
/// <param name="action">One of `feed`, `play`, `sleep`</param>
/// <returns></returns>
[HttpPut("{petId}/action/{action}")]
public IActionResult UpdatePetAction(string petId, string action)
{
try
{
PetAction petAction;
switch (action.ToLower())
{
case "feed":
petAction = PetAction.FEED;
break;
case "play":
petAction = PetAction.PLAY;
break;
case "sleep":
petAction = PetAction.SLEEP;
break;
default:
return BadRequest("Invalid action. Valid actions are: feed, play, sleep");
}
var actionRequest = new PetUpdateActionRequest { Action = petAction };
var updatedPet = petService.UpdatePetAction(petId, userId.ToString(), actionRequest);
return Ok(updatedPet);
}
catch (Exception ex)
{
return NotFound(ex.Message);
}
}
[HttpPut("{petId}/action/gather")]
public IActionResult UpdatePetActionGather(string petId, [FromBody] PetUpdateActionRequest actionRequest)
{
try
{
@@ -44,5 +86,39 @@ namespace pet_companion_api.Controllers
return NotFound(ex.Message);
}
}
[HttpGet("{petId}/resources/gathered")]
public IActionResult GetGatheredResources(string petId)
{
try
{
var resources = petService.GetGatheredResources(petId, userId.ToString());
return Ok(resources);
}
catch (Exception ex)
{
return NotFound(ex.Message);
}
}
[HttpPut("{petId}/resources/collect")]
public IActionResult CollectGatheredResources(string petId)
{
try
{
var updatedPet = petService.UpdatePetResources(petId, userId.ToString());
return Ok(updatedPet);
}
catch (Exception ex)
{
return NotFound(ex.Message);
}
}
[HttpGet("classes")]
public IActionResult GetAllPetClasses()
{
return Ok(petClassService.GetAllPetClasses());
}
}
}