Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Threading.Tasks;
  6. using ExercisesViewModels;
  7. using Microsoft.AspNetCore.Http;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.Extensions.Logging;
  10.  
  11. namespace ExercisesWebsite.Controllers
  12. {
  13. [Route("api/[controller]")]
  14. [ApiController]
  15. public class StudentController : ControllerBase
  16. {
  17. private readonly ILogger _logger;
  18.  
  19. public StudentController(ILogger<StudentController> logger)
  20. {
  21. _logger = logger;
  22. }
  23. [HttpGet("{lastname}")]
  24. public IActionResult GetByLastname(string lastname)
  25. {
  26. try
  27. {
  28. StudentViewModel model = new StudentViewModel();
  29. model.Lastname = lastname;
  30. model.GetByLastName();
  31. return Ok(model);
  32. }
  33. catch(Exception ex)
  34. {
  35. _logger.LogError("Problem in " + GetType().Name + " " + MethodBase.GetCurrentMethod().Name + " " + ex.Message);
  36. return StatusCode(StatusCodes.Status500InternalServerError);
  37. }
  38. }
  39.  
  40. [HttpPut]
  41. public IActionResult Put([FromBody] StudentViewModel viewmodel)
  42. {
  43. try
  44. {
  45. int retVal = viewmodel.Update();
  46. switch (retVal)
  47. {
  48. case 1:
  49. return Ok(new { msg = "Student " + viewmodel.Lastname + " updated!" });
  50. case -1:
  51. return Ok(new { msg = "Student " + viewmodel.Lastname + " not updated!" });
  52. case -2:
  53. return Ok(new { msg = "Data is stale for " + viewmodel.Lastname + " Student not updated!" });
  54. default:
  55. return Ok(new { msg = "Student " + viewmodel.Lastname + " not updated!" });
  56. }
  57. }
  58. catch(Exception ex)
  59. {
  60. _logger.LogError("Problem in " + GetType().Name + " " + MethodBase.GetCurrentMethod().Name + " " + ex.Message);
  61. return StatusCode(StatusCodes.Status500InternalServerError);
  62. }
  63. }
  64.  
  65. [HttpGet]
  66. public IActionResult GetAll()
  67. {
  68. try
  69. {
  70. StudentViewModel viewmodal = new StudentViewModel();
  71. List<StudentViewModel> allstudents = viewmodal.GetAll();
  72. return Ok(allstudents);
  73. }
  74. catch(Exception ex)
  75. {
  76. _logger.LogError("Problem in " + GetType().Name + " " + MethodBase.GetCurrentMethod().Name + " " + ex.Message);
  77. return StatusCode(StatusCodes.Status500InternalServerError);
  78. }
  79. }
  80.  
  81. [HttpPost]
  82. public IActionResult Post(StudentViewModel vm)
  83. {
  84. try
  85. {
  86. vm.Add();
  87. return vm.Id > 1
  88. ? Ok(new { msg = "Student " + vm.Lastname + " added!" })
  89. : Ok(new { msg = "Student " + vm.Lastname + " not added!" });
  90. }
  91. catch(Exception ex)
  92. {
  93. _logger.LogError("Problem in " + GetType().Name + " " + MethodBase.GetCurrentMethod().Name + " " + ex.Message);
  94. return StatusCode(StatusCodes.Status500InternalServerError);
  95. }
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement