Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.34 KB | None | 0 0
  1. using System;
  2. using Microsoft.AspNetCore.Mvc;
  3. using HelpdeskViewModels;
  4. using Microsoft.AspNetCore.Http;
  5. using System.Reflection;
  6. using Microsoft.Extensions.Logging;
  7. using System.Collections.Generic;
  8.  
  9. namespace CasestudyWebsite.Controllers
  10. {
  11.     [Route("api/[controller]")]
  12.     [ApiController]
  13.     public class EmployeeController : ControllerBase
  14.     {
  15.         private readonly ILogger _logger;
  16.  
  17.         public EmployeeController(ILogger<EmployeeController> logger)
  18.         {
  19.             _logger = logger;
  20.         }
  21.         [HttpGet]
  22.         public IActionResult GetAll()
  23.         {
  24.             try
  25.             {
  26.                 EmployeeViewModel viewmodel = new EmployeeViewModel();
  27.                 List<EmployeeViewModel> allEmployees = viewmodel.GetAll();
  28.                 return Ok(allEmployees);
  29.             }
  30.             catch (Exception ex)
  31.             {
  32.                 _logger.LogError("Problem in " + GetType().Name + " " + MethodBase.GetCurrentMethod().Name + " " + ex.Message);
  33.                 return StatusCode(StatusCodes.Status500InternalServerError);
  34.             }
  35.  
  36.         }
  37.         [HttpDelete("{id}")]
  38.         public IActionResult Delete(int id)
  39.         {
  40.             try
  41.             {
  42.                 EmployeeViewModel viewmodel = new EmployeeViewModel();
  43.                 viewmodel.Id = id;
  44.                 return viewmodel.Delete() == 1
  45.                     ? Ok(new { msg = "Employee" + viewmodel.LastName + " not deleted!" })
  46.                     : Ok(new { msg = "Employee" + viewmodel.LastName + " deleted!" });
  47.             }
  48.             catch (Exception ex)
  49.             {
  50.                 _logger.LogError("Problem in " + GetType().Name + " " +
  51.                    MethodBase.GetCurrentMethod().Name + " " + ex.Message);
  52.                 return StatusCode(StatusCodes.Status500InternalServerError); //something went wrong
  53.             }
  54.         }
  55.         [HttpPut]
  56.         public IActionResult Put([FromBody] EmployeeViewModel viewModel)
  57.         {
  58.             try
  59.             {
  60.                 int retVal = viewModel.Update();
  61.                 switch (retVal)
  62.                 {
  63.                     case 1:
  64.                         return Ok(new { msg = "Employee " + viewModel.LastName + " updated:" });
  65.                     case -1:
  66.                         return Ok(new { msg = "Employee" + viewModel.LastName + " not updated:" });
  67.                     case -2:
  68.                         return Ok(new { msg = "Data is stale for " + viewModel.LastName + ", Employee not updated" });
  69.                     default:
  70.                         return Ok(new { msg = "Employee " + viewModel.LastName + " not updated!" });
  71.                 }
  72.             }
  73.             catch (Exception ex)
  74.             {
  75.                 _logger.LogError("Problem in " + GetType().Name + " " +
  76.                     MethodBase.GetCurrentMethod().Name + " " + ex.Message);
  77.                 return StatusCode(StatusCodes.Status500InternalServerError); // something went wrong
  78.             }
  79.         }
  80.         [HttpGet("{email}")]
  81.  
  82.         public IActionResult GetByEmail(string email)
  83.         {
  84.             try
  85.             {
  86.                 EmployeeViewModel viewmodel = new EmployeeViewModel();
  87.                 viewmodel.Email = email;
  88.                 viewmodel.GetByEmail();
  89.                 return Ok(viewmodel);
  90.             }
  91.             catch (Exception ex)
  92.             {
  93.                 _logger.LogError("Problem in " + GetType().Name + " " +
  94.                     MethodBase.GetCurrentMethod().Name + " " + ex.Message);
  95.                 return StatusCode(StatusCodes.Status500InternalServerError); // something went wrong
  96.             }
  97.         }
  98.         [HttpPost]
  99.         public IActionResult Post(EmployeeViewModel viewmodel)
  100.         {
  101.             try
  102.             {
  103.                 viewmodel.Add();
  104.                 return viewmodel.Id > 1
  105.                     ? Ok(new { msg = "Student" + viewmodel.LastName + " added!" })
  106.                     : Ok(new { msg = "Student" + viewmodel.LastName + " not added!" });
  107.             }
  108.             catch (Exception ex)
  109.             {
  110.                 _logger.LogError("Problem in " + GetType().Name + " " +
  111.                    MethodBase.GetCurrentMethod().Name + " " + ex.Message);
  112.                 return StatusCode(StatusCodes.Status500InternalServerError); //something went wrong
  113.             }
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement