first commit
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
using BethanysPieShopHRM.Api.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace BethanysPieShopHRM.Api.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class CountryController : Controller
|
||||
{
|
||||
private readonly ICountryRepository _countryRepository;
|
||||
|
||||
public CountryController(ICountryRepository countryRepository)
|
||||
{
|
||||
_countryRepository = countryRepository;
|
||||
}
|
||||
|
||||
// GET: api/<controller>
|
||||
[HttpGet]
|
||||
public IActionResult GetCountries()
|
||||
{
|
||||
return Ok(_countryRepository.GetAllCountries());
|
||||
}
|
||||
|
||||
// GET api/<controller>/5
|
||||
[HttpGet("{id}")]
|
||||
public IActionResult GetCountryById(int id)
|
||||
{
|
||||
return Ok(_countryRepository.GetCountryById(id));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
using BethanysPieShopHRM.Api.Models;
|
||||
using BethanysPieShopHRM.Shared.Domain;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace BethanysPieShopHRM.Api.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class EmployeeController : Controller
|
||||
{
|
||||
private readonly IEmployeeRepository _employeeRepository;
|
||||
private readonly IWebHostEnvironment _webHostEnvironment;
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
public EmployeeController(IEmployeeRepository employeeRepository, IWebHostEnvironment webHostEnvironment, IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_employeeRepository = employeeRepository;
|
||||
_webHostEnvironment = webHostEnvironment;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult GetAllEmployees()
|
||||
{
|
||||
return Ok(_employeeRepository.GetAllEmployees());
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public IActionResult GetEmployeeById(int id)
|
||||
{
|
||||
return Ok(_employeeRepository.GetEmployeeById(id));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult CreateEmployee([FromBody] Employee employee)
|
||||
{
|
||||
if (employee == null)
|
||||
return BadRequest();
|
||||
|
||||
if (employee.FirstName == string.Empty || employee.LastName == string.Empty)
|
||||
{
|
||||
ModelState.AddModelError("Name/FirstName", "The name or first name shouldn't be empty");
|
||||
}
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(ModelState);
|
||||
|
||||
//handle image upload
|
||||
string currenturl = _httpContextAccessor.HttpContext.Request.Host.Value;
|
||||
var path = $"{_webHostEnvironment.WebRootPath}\\uploads\\{employee.ImageName}";
|
||||
var filestream = System.IO.File.Create(path);
|
||||
filestream.Write(employee.ImageContent, 0, employee.ImageContent.Length);
|
||||
filestream.Close();
|
||||
|
||||
employee.ImageName = $"https://{currenturl}/uploads/{employee.ImageName}";
|
||||
|
||||
var createdEmployee = _employeeRepository.AddEmployee(employee);
|
||||
|
||||
return Created("employee", createdEmployee);
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
public IActionResult UpdateEmployee([FromBody] Employee employee)
|
||||
{
|
||||
if (employee == null)
|
||||
return BadRequest();
|
||||
|
||||
if (employee.FirstName == string.Empty || employee.LastName == string.Empty)
|
||||
{
|
||||
ModelState.AddModelError("Name/FirstName", "The name or first name shouldn't be empty");
|
||||
}
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(ModelState);
|
||||
|
||||
var employeeToUpdate = _employeeRepository.GetEmployeeById(employee.EmployeeId);
|
||||
|
||||
if (employeeToUpdate == null)
|
||||
return NotFound();
|
||||
|
||||
_employeeRepository.UpdateEmployee(employee);
|
||||
|
||||
return NoContent(); //success
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public IActionResult DeleteEmployee(int id)
|
||||
{
|
||||
if (id == 0)
|
||||
return BadRequest();
|
||||
|
||||
var employeeToDelete = _employeeRepository.GetEmployeeById(id);
|
||||
if (employeeToDelete == null)
|
||||
return NotFound();
|
||||
|
||||
_employeeRepository.DeleteEmployee(id);
|
||||
|
||||
return NoContent();//success
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using BethanysPieShopHRM.Api.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace BethanysPieShopHRM.Api.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class JobCategoryController : Controller
|
||||
{
|
||||
private readonly IJobCategoryRepository _jobCategoryRepository;
|
||||
|
||||
public JobCategoryController(IJobCategoryRepository jobCategoryRepository)
|
||||
{
|
||||
_jobCategoryRepository = jobCategoryRepository;
|
||||
}
|
||||
|
||||
|
||||
// GET: api/<controller>
|
||||
[HttpGet]
|
||||
public IActionResult GetJobCategories()
|
||||
{
|
||||
return Ok(_jobCategoryRepository.GetAllJobCategories());
|
||||
}
|
||||
|
||||
// GET api/<controller>/5
|
||||
[HttpGet("{id}")]
|
||||
public IActionResult GetJobCategoryById(int id)
|
||||
{
|
||||
return Ok(_jobCategoryRepository.GetJobCategoryById(id));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user