Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Microsoft.AspNetCore.Mvc;
- using HelpdeskViewModels;
- using Microsoft.AspNetCore.Http;
- using System.Reflection;
- using Microsoft.Extensions.Logging;
- using System.Collections.Generic;
- namespace CasestudyWebsite.Controllers
- {
- [Route("api/[controller]")]
- [ApiController]
- public class EmployeeController : ControllerBase
- {
- private readonly ILogger _logger;
- public EmployeeController(ILogger<EmployeeController> logger)
- {
- _logger = logger;
- }
- [HttpGet]
- public IActionResult GetAll()
- {
- try
- {
- EmployeeViewModel viewmodel = new EmployeeViewModel();
- List<EmployeeViewModel> allEmployees = viewmodel.GetAll();
- return Ok(allEmployees);
- }
- catch (Exception ex)
- {
- _logger.LogError("Problem in " + GetType().Name + " " + MethodBase.GetCurrentMethod().Name + " " + ex.Message);
- return StatusCode(StatusCodes.Status500InternalServerError);
- }
- }
- [HttpDelete("{id}")]
- public IActionResult Delete(int id)
- {
- try
- {
- EmployeeViewModel viewmodel = new EmployeeViewModel();
- viewmodel.Id = id;
- return viewmodel.Delete() == 1
- ? Ok(new { msg = "Employee" + viewmodel.LastName + " not deleted!" })
- : Ok(new { msg = "Employee" + viewmodel.LastName + " deleted!" });
- }
- catch (Exception ex)
- {
- _logger.LogError("Problem in " + GetType().Name + " " +
- MethodBase.GetCurrentMethod().Name + " " + ex.Message);
- return StatusCode(StatusCodes.Status500InternalServerError); //something went wrong
- }
- }
- [HttpPut]
- public IActionResult Put([FromBody] EmployeeViewModel viewModel)
- {
- try
- {
- int retVal = viewModel.Update();
- switch (retVal)
- {
- case 1:
- return Ok(new { msg = "Employee " + viewModel.LastName + " updated:" });
- case -1:
- return Ok(new { msg = "Employee" + viewModel.LastName + " not updated:" });
- case -2:
- return Ok(new { msg = "Data is stale for " + viewModel.LastName + ", Employee not updated" });
- default:
- return Ok(new { msg = "Employee " + viewModel.LastName + " not updated!" });
- }
- }
- catch (Exception ex)
- {
- _logger.LogError("Problem in " + GetType().Name + " " +
- MethodBase.GetCurrentMethod().Name + " " + ex.Message);
- return StatusCode(StatusCodes.Status500InternalServerError); // something went wrong
- }
- }
- [HttpGet("{email}")]
- public IActionResult GetByEmail(string email)
- {
- try
- {
- EmployeeViewModel viewmodel = new EmployeeViewModel();
- viewmodel.Email = email;
- viewmodel.GetByEmail();
- return Ok(viewmodel);
- }
- catch (Exception ex)
- {
- _logger.LogError("Problem in " + GetType().Name + " " +
- MethodBase.GetCurrentMethod().Name + " " + ex.Message);
- return StatusCode(StatusCodes.Status500InternalServerError); // something went wrong
- }
- }
- [HttpPost]
- public IActionResult Post(EmployeeViewModel viewmodel)
- {
- try
- {
- viewmodel.Add();
- return viewmodel.Id > 1
- ? Ok(new { msg = "Student" + viewmodel.LastName + " added!" })
- : Ok(new { msg = "Student" + viewmodel.LastName + " not added!" });
- }
- catch (Exception ex)
- {
- _logger.LogError("Problem in " + GetType().Name + " " +
- MethodBase.GetCurrentMethod().Name + " " + ex.Message);
- return StatusCode(StatusCodes.Status500InternalServerError); //something went wrong
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement