-
Notifications
You must be signed in to change notification settings - Fork 240
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
1ra Actividad - CRUD Provider Controller, ProviderDTO, ProviderService #131
base: master
Are you sure you want to change the base?
Changes from 1 commit
ec801e6
1a73142
e9c0779
5d5c192
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,11 +20,12 @@ public class ProviderController : ControllerBase | |
|
||
private readonly ProviderService service; | ||
private readonly IMapper mapper; | ||
|
||
|
||
public ProviderController(ProviderService service, IMapper mapper) | ||
{ | ||
this.service = service ?? throw new ArgumentException(nameof(service)); | ||
this.mapper = mapper ?? throw new ArgumentException(nameof(mapper)); | ||
this.mapper = mapper ?? throw new ArgumentException(nameof(mapper)); | ||
} | ||
|
||
|
||
|
@@ -35,14 +36,19 @@ public ProviderController(ProviderService service, IMapper mapper) | |
[HttpGet] | ||
public ActionResult<IEnumerable<ProviderDTO>> Get() | ||
{ | ||
logger.LogInformation("Mensaje Log de Get"); | ||
try | ||
{ | ||
var result = service.GetAll(); | ||
return mapper.Map<IEnumerable<ProviderDTO>>(result).ToList(); | ||
var provider = service.GetAll(); | ||
if (provider == null) | ||
{ | ||
return Ok(new { statusCode = "404", result = "No hay datos en Proveedores" }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Va mejor lo de devolver el not found, pero la idea es que sea el HttpStatus correcto, si haces un There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. De acuerdo, ya lo cambié |
||
} | ||
return mapper.Map<IEnumerable<ProviderDTO>>(provider).ToList(); | ||
} | ||
catch (Exception) | ||
{ | ||
return StatusCode(500); | ||
catch (Exception ex) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dependiendo del tipo de exception que captures, la respuesta va a ser un 400 Bad Request o un 500 Internal server error. La diferencia es que un 400 el error esta del lado del cliente, un 500 es un error del lado del servidor. Simplemente dejando la exception sin handlear haces que el cliente reciba el 500. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totalmente de acuerdo, no lo habia pensado. |
||
return BadRequest(new { statusCode = "400", errorMessage = ex.Message }); | ||
} | ||
} | ||
|
||
|
@@ -53,8 +59,21 @@ public ActionResult<IEnumerable<ProviderDTO>> Get() | |
/// <returns>A <see cref="ProviderDTO"/></returns> | ||
[HttpGet("{id}")] | ||
public ActionResult<ProviderDTO> Get(string id) | ||
{ | ||
return Ok(mapper.Map<ProviderDTO>(service.Get(id))); | ||
{ | ||
try | ||
{ | ||
var provider = mapper.Map<ProviderDTO>(service.Get(id)); | ||
if (provider == null) | ||
{ | ||
return Ok(new { statusCode = "404", result = "El Proveedor no fué encontrado" }); | ||
} | ||
return Ok(provider); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return BadRequest(new { statusCode = "400", errorMessage = ex.Message }); | ||
} | ||
|
||
} | ||
|
||
/// <summary> | ||
|
@@ -75,18 +94,23 @@ public Provider Post([FromBody] ProviderDTO value) | |
/// <param name="id">Provider id to edit.</param> | ||
/// <param name="value">Prodvider information.</param> | ||
[HttpPut("{id}")] | ||
public void Put(string id, [FromBody] ProviderDTO value) | ||
{ | ||
var provider = service.Get(id); | ||
public ActionResult Put(string id, [FromBody] ProviderDTO value) | ||
{ | ||
TryValidateModel(value); | ||
try | ||
{ | ||
var provider = service.Get(id); | ||
if (provider == null) | ||
{ | ||
return Ok(new { statusCode = "404", result = "El Proveedor no fué encontrado" }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aca lo mismo que el la linea 45. Con un There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Listo |
||
} | ||
mapper.Map<ProviderDTO, Provider>(value, provider); | ||
service.Update(provider); | ||
return Ok(new { statusCode = "200", result = "Proveedor modificado exitosamente" }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generalmente para los POST (creando un objeto nuevo) la respuesta es 201 Created, para los PUT (updates) y DELETE, se puede usar un 200 OK, o tambien se suele usar un 204 No Content. |
||
} | ||
catch (Exception ex) | ||
{ | ||
var msg = ex.Message; | ||
{ | ||
return BadRequest(new { statusCode = "400", result = ex.Message }); | ||
} | ||
} | ||
|
||
|
@@ -97,17 +121,23 @@ public void Put(string id, [FromBody] ProviderDTO value) | |
[HttpDelete("{id}")] | ||
public ActionResult Delete(string id) | ||
{ | ||
var provider = service.Get(id); | ||
|
||
if (provider is null) | ||
return NotFound(); | ||
try | ||
{ | ||
var provider = service.Get(id); | ||
if (provider is null) | ||
return Ok(new { statusCode = "404", result = "El Proveedor no fué encontrado" }); | ||
|
||
service.Delete(provider); | ||
return Ok(); | ||
service.Delete(provider); | ||
return Ok(new { statusCode = "200", result = "Proveedor eliminado exitosamente" }); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return BadRequest(new { statusCode = "400", result = ex.Message }); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Search by Id, Name, Phone, Email | ||
/// Search by Name, Phone, Email | ||
/// </summary> | ||
/// <param name="model">Provider information.</param> | ||
/// <returns>A <see cref="ProviderSearchDTO"/></returns> | ||
|
@@ -117,14 +147,6 @@ public ActionResult Search([FromQuery] ProviderSearchDTO model) | |
{ | ||
Expression<Func<Provider, bool>> filter = x => !string.IsNullOrWhiteSpace(x.Id); | ||
|
||
// Search by Id | ||
if (!string.IsNullOrWhiteSpace(model.Id)) | ||
{ | ||
filter = filter.AndOrCustom( | ||
x => x.Id.ToUpper().Contains(model.Id.ToUpper()), | ||
model.Condition.Equals(ActionDto.AND)); | ||
} | ||
|
||
// Search by Name | ||
if (!string.IsNullOrWhiteSpace(model.Name)) | ||
{ | ||
|
@@ -149,8 +171,11 @@ public ActionResult Search([FromQuery] ProviderSearchDTO model) | |
model.Condition.Equals(ActionDto.AND)); | ||
} | ||
|
||
|
||
var provider = service.Search(filter); | ||
|
||
if(provider.Count()==0) | ||
return Ok(new { statusCode = "404", result = "No se encontró información del Proveedor" }); | ||
|
||
return Ok(provider); | ||
} | ||
} | ||
|
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.
Fijate que el Startup configura un logger que podes inyectar y despues usar para registrar eventos y errores.