Skip to content

Commit

Permalink
refactor: organized mvc designer in controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Joaoms98 committed Feb 11, 2023
1 parent 7c3be9d commit 1453134
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 208 deletions.
136 changes: 67 additions & 69 deletions PokedexApi.Web/Controllers/EvolutionController.cs
Original file line number Diff line number Diff line change
@@ -1,77 +1,75 @@
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using PokedexApi.Domain.Dtos;
using PokedexApi.Domain.Interfaces;
using PokedexApi.Domain.Responses;
using PokedexApi.Web.FormRequest;
using PokedexApi.Web.Resources;
// using AutoMapper;
// using Microsoft.AspNetCore.Mvc;
// using PokedexApi.Domain.Dtos;
// using PokedexApi.Domain.Interfaces;
// using PokedexApi.Web.FormRequest;

namespace PokedexApi.Web.Controllers
{
[ApiController]
[Route("evolution")]
public class EvolutionController : ControllerBase
{
private readonly IEvolutionRepository _repository;
private readonly IMapper _mapper;
// namespace PokedexApi.Web.Controllers
// {
// [ApiController]
// [Route("evolution")]
// public class EvolutionController : ControllerBase
// {
// private readonly IEvolutionRepository _repository;
// private readonly IMapper _mapper;

public EvolutionController(IEvolutionRepository repository, IMapper mapper)
{
_repository = repository;
_mapper = mapper;
}
// public EvolutionController(IEvolutionRepository repository, IMapper mapper)
// {
// _repository = repository;
// _mapper = mapper;
// }

[HttpPost("add")]
public async Task<IActionResult> AddAsync([FromBody] EvolutionAddFormRequest payload)
{
try
{
var result = await _repository.AddAsync(new EvolutionAddDTO
{
PokemonId = payload.PokemonId,
PreEvolution = payload.PreEvolution,
EvolutionForm = payload.EvolutionForm
});
// [HttpPost("add")]
// public async Task<IActionResult> AddAsync([FromBody] EvolutionAddFormRequest payload)
// {
// try
// {
// var result = await _repository.AddAsync(new EvolutionAddDTO
// {
// PokemonId = payload.PokemonId,
// PreEvolution = payload.PreEvolution,
// EvolutionForm = payload.EvolutionForm
// });

return Created("Evolution has been registered", result);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
// return Created("Evolution has been registered", result);
// }
// catch (Exception ex)
// {
// return BadRequest(ex.Message);
// }
// }

[HttpGet("{pokemonId}")]
public async Task<IActionResult> GetByPokemonId([FromRoute] int pokemonId)
{
try
{
var result = await _repository.GetByIdAsync(new EvolutionGetByIdDTO
{
PokemonId = pokemonId
});
// [HttpGet("{pokemonId}")]
// public async Task<IActionResult> GetByPokemonId([FromRoute] int pokemonId)
// {
// try
// {
// var result = await _repository.GetByIdAsync(new EvolutionGetByIdDTO
// {
// PokemonId = pokemonId
// });

return Ok(_mapper.Map<EvolutionResource>(result));
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
// return Ok(_mapper.Map<EvolutionResource>(result));
// }
// catch (Exception ex)
// {
// return BadRequest(ex.Message);
// }
// }

[HttpDelete("{pokemonId}/remove")]
public async Task<IActionResult> RemoveByPokemonId([FromRoute] Guid pokemonId)
{
try
{
await _repository.DeleteAsync(pokemonId);
// [HttpDelete("{pokemonId}/remove")]
// public async Task<IActionResult> RemoveByPokemonId([FromRoute] Guid pokemonId)
// {
// try
// {
// await _repository.DeleteAsync(pokemonId);

return NoContent();
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
}
}
// return NoContent();
// }
// catch (Exception ex)
// {
// return BadRequest(ex.Message);
// }
// }
// }
// }
14 changes: 7 additions & 7 deletions PokedexApi.Web/Controllers/PokemonController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
using PokedexApi.Domain.Dtos;
using PokedexApi.Domain.Interfaces;
using PokedexApi.Web.FormRequest;
using PokedexApi.Web.Resources;
using PokedexApi.Web.Models;

namespace PokedexApi.Web.Controllers
{
[ApiController]
[Route("pokemon")]
public class PokemonController : ControllerBase
public class PokemonController : Controller
{
private readonly IPokemonRepository _repository;
private readonly IMapper _mapper;
Expand All @@ -21,13 +21,13 @@ public PokemonController(IPokemonRepository repository, IMapper mapper)
}

[HttpGet("index")]
public async Task<IActionResult> index ([FromQuery] PokemonListAllFormRequest payload)
public async Task<IActionResult> Index ([FromQuery] PokemonListAllFormRequest payload)
{
try
{
var result = await _repository.ListAllAsync(payload.Page);

return Ok(_mapper.Map<IEnumerable<PokemonResource>>(result));
return View(_mapper.Map<IEnumerable<PokemonModel>>(result));
}
catch(Exception ex)
{
Expand All @@ -42,7 +42,7 @@ public async Task<IActionResult> GetByDexNumber ([FromRoute] int dexNumber)
{
var result = await _repository.GetByDexNumberAsync(dexNumber);

return Ok(_mapper.Map<PokemonResource>(result));
return View(_mapper.Map<PokemonModel>(result));
}
catch(Exception ex)
{
Expand All @@ -62,7 +62,7 @@ public async Task<IActionResult> ListWithParams ([FromQuery] PokemonGetWithParam
Page = payload.Page,
});

return Ok(_mapper.Map<IEnumerable<PokemonResource>>(result));
return View(_mapper.Map<IEnumerable<PokemonModel>>(result));
}
catch(Exception ex)
{
Expand Down
155 changes: 69 additions & 86 deletions PokedexApi.Web/Controllers/SpecialStageController.cs
Original file line number Diff line number Diff line change
@@ -1,94 +1,77 @@
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using PokedexApi.Domain.Dtos;
using PokedexApi.Domain.Interfaces;
using PokedexApi.Web.FormRequest;
// using AutoMapper;
// using Microsoft.AspNetCore.Mvc;
// using PokedexApi.Domain.Dtos;
// using PokedexApi.Domain.Interfaces;
// using PokedexApi.Web.FormRequest;

namespace PokedexApi.Web.Controllers
{
[ApiController]
[Route("special-stage")]
public class SpecialStageController : ControllerBase
{
private readonly ISpecialStageRepository _repository;
private readonly IMapper _mapper;
// namespace PokedexApi.Web.Controllers
// {
// [ApiController]
// [Route("special-stage")]
// public class SpecialStageController : ControllerBase
// {
// private readonly ISpecialStageRepository _repository;

public SpecialStageController(ISpecialStageRepository repository, IMapper mapper)
{
_repository = repository;
_mapper = mapper;
}
// public SpecialStageController(ISpecialStageRepository repository, IMapper mapper)
// {
// _repository = repository;
// }

[HttpPost("add")]
public async Task<IActionResult> Add ([FromBody] SpecialStageCreateFormRequest payload)
{
try
{
var result = await _repository.AddAsync(new SpecialStageAddDTO
{
PokemonId = payload.PokemonId,
DexNumber = payload.DexNumber,
Image = payload.Image,
Description = payload.Description,
Height = payload.Height,
Weight = payload.Weight,
Region = payload.Region
});
// [HttpPost("add")]
// public async Task<IActionResult> Add ([FromBody] SpecialStageCreateFormRequest payload)
// {
// try
// {
// var result = await _repository.AddAsync(new SpecialStageAddDTO
// {
// PokemonId = payload.PokemonId,
// DexNumber = payload.DexNumber,
// Image = payload.Image,
// Description = payload.Description,
// Height = payload.Height,
// Weight = payload.Weight,
// Region = payload.Region
// });

return Created("Special Stage has been registered", result);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
// return Created("Special Stage has been registered", result);
// }
// catch (Exception ex)
// {
// return BadRequest(ex.Message);
// }
// }

[HttpGet("{id}")]
public async Task<IActionResult> GetById ([FromRoute] int id)
{
try
{
var result = await _repository.GetByIdAsync(id);
// [HttpGet("all")]
// public async Task<IActionResult> All ([FromQuery] PokemonListAllFormRequest payload)
// {
// try
// {
// var result = await _repository.AllAsync(new SpecialStageListAllDTO
// {
// Page = payload.Page,
// });

return Ok(result);
}
catch(Exception ex)
{
return BadRequest(ex.Message);
}
}
// return Ok(result);
// }
// catch(Exception ex)
// {
// return BadRequest(ex.Message);
// }
// }

[HttpGet("all")]
public async Task<IActionResult> All ([FromQuery] PokemonListAllFormRequest payload)
{
try
{
var result = await _repository.AllAsync(new SpecialStageListAllDTO
{
Page = payload.Page,
});
// [HttpDelete("{id}/remove")]
// public async Task<IActionResult> Remove ([FromRoute] Guid id)
// {
// try
// {
// await _repository.DeleteAsync(id);

return Ok(result);
}
catch(Exception ex)
{
return BadRequest(ex.Message);
}
}

[HttpDelete("{id}/remove")]
public async Task<IActionResult> Remove ([FromRoute] Guid id)
{
try
{
await _repository.DeleteAsync(id);

return NoContent();
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
}
}
// return NoContent();
// }
// catch (Exception ex)
// {
// return BadRequest(ex.Message);
// }
// }
// }
// }
7 changes: 2 additions & 5 deletions PokedexApi.Web/Mapper/DomainProfile.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
using AutoMapper;
using PokedexApi.Domain.Entities;
using PokedexApi.Domain.Responses;
using PokedexApi.Web.Resources;
using PokedexApi.Web.Models;

namespace PokedexApi.Web.Mapper
{
public class DomainProfile : Profile
{
public DomainProfile()
{
CreateMap<Pokemon, PokemonResource>(MemberList.Destination);
CreateMap<Pokemon, PokemonEvolutionResource>(MemberList.Destination);
CreateMap<EvolutionResponse, EvolutionResource>(MemberList.Destination);
CreateMap<Pokemon, PokemonModel>(MemberList.Destination);
}
}
}
15 changes: 15 additions & 0 deletions PokedexApi.Web/Models/PokemonModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using PokedexApi.Core.Enums;
namespace PokedexApi.Web.Models;

public class PokemonModel
{
public int DexNumber { get; set; }
public string Name { get; set; }
public string Image { get; set; }
public string Description { get; set; }
public double Height { get; set; }
public double Weight { get; set; }
public GenderEnum Gender { get; set; }
public RarityEnum Rarity { get; set; }
public string Region { get; set; }
}
Loading

0 comments on commit 1453134

Please sign in to comment.