Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Threading.Tasks;
- using ExercisesViewModels;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Logging;
- namespace ExercisesWebsite.Controllers
- {
- [Route("api/[controller]")]
- [ApiController]
- public class StudentController : ControllerBase
- {
- private readonly ILogger _logger;
- public StudentController(ILogger<StudentController> logger)
- {
- _logger = logger;
- }
- [HttpGet("{lastname}")]
- public IActionResult GetByLastname(string lastname)
- {
- try
- {
- StudentViewModel model = new StudentViewModel();
- model.Lastname = lastname;
- model.GetByLastName();
- return Ok(model);
- }
- catch(Exception ex)
- {
- _logger.LogError("Problem in " + GetType().Name + " " + MethodBase.GetCurrentMethod().Name + " " + ex.Message);
- return StatusCode(StatusCodes.Status500InternalServerError);
- }
- }
- [HttpPut]
- public IActionResult Put([FromBody] StudentViewModel viewmodel)
- {
- try
- {
- int retVal = viewmodel.Update();
- switch (retVal)
- {
- case 1:
- return Ok(new { msg = "Student " + viewmodel.Lastname + " updated!" });
- case -1:
- return Ok(new { msg = "Student " + viewmodel.Lastname + " not updated!" });
- case -2:
- return Ok(new { msg = "Data is stale for " + viewmodel.Lastname + " Student not updated!" });
- default:
- return Ok(new { msg = "Student " + viewmodel.Lastname + " not updated!" });
- }
- }
- catch(Exception ex)
- {
- _logger.LogError("Problem in " + GetType().Name + " " + MethodBase.GetCurrentMethod().Name + " " + ex.Message);
- return StatusCode(StatusCodes.Status500InternalServerError);
- }
- }
- [HttpGet]
- public IActionResult GetAll()
- {
- try
- {
- StudentViewModel viewmodal = new StudentViewModel();
- List<StudentViewModel> allstudents = viewmodal.GetAll();
- return Ok(allstudents);
- }
- catch(Exception ex)
- {
- _logger.LogError("Problem in " + GetType().Name + " " + MethodBase.GetCurrentMethod().Name + " " + ex.Message);
- return StatusCode(StatusCodes.Status500InternalServerError);
- }
- }
- [HttpPost]
- public IActionResult Post(StudentViewModel vm)
- {
- try
- {
- vm.Add();
- return vm.Id > 1
- ? Ok(new { msg = "Student " + vm.Lastname + " added!" })
- : Ok(new { msg = "Student " + vm.Lastname + " not added!" });
- }
- catch(Exception ex)
- {
- _logger.LogError("Problem in " + GetType().Name + " " + MethodBase.GetCurrentMethod().Name + " " + ex.Message);
- return StatusCode(StatusCodes.Status500InternalServerError);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement