Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
taevt-iscte committed Jun 21, 2024
1 parent 14cf939 commit 41b76f5
Show file tree
Hide file tree
Showing 26 changed files with 982 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
!**/.gitignore
!.git/HEAD
!.git/config
!.git/packed-refs
!.git/refs/heads/**
114 changes: 114 additions & 0 deletions Controllers/EmployeeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Restaurant_Management.Dto;
using Restaurant_Management.Interfaces;
using Restaurant_Management.Models;

namespace Restaurant_Management.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EmployeeController(IEmployeeRepository _employeeRepository, IRestaurantRepository _restaurantRepository, IMapper _mapper) : ControllerBase
{
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<Employee>))]
public IActionResult GetEmployees()
{
return Ok(_mapper.Map<EmployeeDto>(_employeeRepository.GetEmployees()));
}

[HttpGet("{id}")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(Employee))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult GetEmployee(int id)
{
if (!ModelState.IsValid || id <= 0)
return BadRequest(ModelState);
EmployeeDto employeeDto = _mapper.Map<EmployeeDto>(_employeeRepository.GetEmployee(id));
if (employeeDto == null)
return NotFound();
return Ok(employeeDto);
}

[HttpGet("{id}/income")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(float))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult GetEmployeeGrossIncome(int id)
{
if (!ModelState.IsValid || id <= 0)
return BadRequest(ModelState);
float? income = _employeeRepository.GetEmployeeGrossIncome(id);
if (income == null)
return NotFound();
return Ok(income);
}

[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult CreateEmployee([FromQuery] int restaurantId, [FromBody] EmployeeDto employeeDto)
{
if (!ModelState.IsValid || employeeDto == null)
return BadRequest(ModelState);
if (_employeeRepository.GetEmployees().Where(e => e.Name.Equals(employeeDto.Name, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault() != null)
{
ModelState.AddModelError("Existing", "Employee already exists");
return BadRequest(ModelState);
}
Employee employee = _mapper.Map<Employee>(employeeDto);
Restaurant restaurant = _restaurantRepository.GetRestaurant(restaurantId);
if (restaurant == null)
return NotFound();
employee.Restaurant = restaurant;
int id = _employeeRepository.CreateEmployee(employee);
if (id < 0)
{
ModelState.AddModelError("Internal", "Something went wrong while saving");
return StatusCode(StatusCodes.Status500InternalServerError, ModelState);
}
return Ok(id);
}

[HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult DeleteEmployee(int id)
{
if (!ModelState.IsValid || id <= 0)
return BadRequest(ModelState);
Employee? emp = _employeeRepository.GetEmployee(id);
if (emp == null)
return NotFound();
if (!_employeeRepository.DeleteEmployee(emp))
{
ModelState.AddModelError("Internal", "Something went wrong while saving");
return StatusCode(StatusCodes.Status500InternalServerError, ModelState);
}
return NoContent();
}

[HttpPut("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult UpdateEmployee(int id, Employee emp)
{
if (emp == null || id <= 0 || !ModelState.IsValid)
return BadRequest(ModelState);
Employee? employee = _employeeRepository.GetEmployee(id);
if (employee == null)
return NotFound();
if (!_employeeRepository.UpdateEmployee(employee))
{
ModelState.AddModelError("Internal", "Something went wrong while saving");
return StatusCode(StatusCodes.Status500InternalServerError, ModelState);
}
return NoContent();
}
}
}
117 changes: 117 additions & 0 deletions Controllers/RestaurantController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using Microsoft.AspNetCore.Mvc;
using Restaurant_Management.Models;
using AutoMapper;
using Restaurant_Management.Dto;
using Restaurant_Management.Interfaces;

namespace Restaurant_Management.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class RestaurantController(ILogger<RestaurantController> logger, IRestaurantRepository context, IEmployeeRepository employeeRepository, IMapper mapper) : ControllerBase
{
private readonly IRestaurantRepository _restaurantRepository = context;
private readonly ILogger<RestaurantController> _logger = logger;
private readonly IEmployeeRepository _employeeRepository = employeeRepository;
private readonly IMapper _mapper = mapper;

[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<Restaurant>))]
public IActionResult GetRestaurants()
{
return Ok(_mapper.Map<List<RestaurantDto>>(_restaurantRepository.GetRestaurants()));
}

[HttpGet("{id}")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(Restaurant))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult GetRestaurant(int id)
{
RestaurantDto? rest = _mapper.Map<RestaurantDto>(_restaurantRepository.GetRestaurant(id));
if (rest == null)
return NotFound();
if (!ModelState.IsValid)
return BadRequest();
return Ok(rest!);
}

[HttpGet("{id}/employees")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<Employee>))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult GetRestaurantEmployees(int id)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
Restaurant rest = _restaurantRepository.GetRestaurant(id);
if (rest == null)
return NotFound();
return Ok(_mapper.Map<List<EmployeeDto>>(_employeeRepository.GetEmployeesByRestaurant(id)));
}

[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public IActionResult CreateRestaurant([FromBody] RestaurantDto restDto)
{
if (restDto == null || !ModelState.IsValid)
return BadRequest(ModelState);
if (_restaurantRepository.GetRestaurants().Where(rest => rest.Name.Equals(restDto.Name, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault() != null)
{
ModelState.AddModelError("Existent", "Restaurant already exists");
return StatusCode(StatusCodes.Status422UnprocessableEntity, ModelState);
}
Restaurant rest = _mapper.Map<Restaurant>(restDto);
rest.CreatedDate = DateTime.Now;
int id = _restaurantRepository.CreateRestaurant(rest);
if (id < 0)
{
ModelState.AddModelError("Internal", "Something went wrong while saving");
return StatusCode(StatusCodes.Status500InternalServerError, ModelState);
}
return Ok(id);
}

[HttpPut("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult UpdateRestaurant(int id, [FromBody]RestaurantDto restaurantDto)
{
if (!ModelState.IsValid || restaurantDto == null || restaurantDto.Id != id)
{
return BadRequest(ModelState);
}
if (!_restaurantRepository.GetRestaurants().Where(r => r.Id == id).Any())
{
return NotFound();
}
Restaurant rest = _mapper.Map<Restaurant>(restaurantDto);
if (!_restaurantRepository.UpdateRestaurant(rest))
{
ModelState.AddModelError("Internal", "Something went wrong saving");
return StatusCode(StatusCodes.Status500InternalServerError, ModelState);
}
return Ok("Updated");
}

[HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult DeleteRestaurant(int id)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
if (_restaurantRepository.GetRestaurant(id) == null)
return NotFound();
if (!_restaurantRepository.RemoveRestaurant(id))
{
ModelState.AddModelError("Internal", "Something went wrong while deleting");
return StatusCode(StatusCodes.Status500InternalServerError, ModelState);
}
return Ok("Deleted");
}
}
}
11 changes: 11 additions & 0 deletions Data/ApplicationDBContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.EntityFrameworkCore;
using Restaurant_Management.Models;

namespace Restaurant_Management.Data
{
public class ApplicationDBContext(DbContextOptions<ApplicationDBContext> options) : DbContext(options)
{
public DbSet<Restaurant> Restaurants { get; set; }
public DbSet<Employee> Employees { get; set; }
}
}
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["Restaurant Management.csproj", "."]
RUN dotnet restore "./././Restaurant Management.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "./Restaurant Management.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./Restaurant Management.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Restaurant Management.dll"]
10 changes: 10 additions & 0 deletions Dto/EmployeeDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Restaurant_Management.Dto
{
public class EmployeeDto
{
public int Id { get; set; }
public string Name { get; set; }
public string? Address { get; set; }
public float GrossIncome { get; set; }
}
}
11 changes: 11 additions & 0 deletions Dto/RestaurantDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Restaurant_Management.Dto
{
public class RestaurantDto
{
public int Id { get; set; }
public string Name { get; set; }
public string? Description { get; set; }
public string Contact { get; set; }
public DateTime CreatedDate { get; set; }
}
}
15 changes: 15 additions & 0 deletions Helper/MappingProfiles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using AutoMapper;
using Restaurant_Management.Dto;
using Restaurant_Management.Models;

namespace Restaurant_Management.Helper
{
public class MappingProfiles : Profile
{
public MappingProfiles()
{
CreateMap<Restaurant, RestaurantDto>().ReverseMap();
CreateMap<Employee, EmployeeDto>().ReverseMap();
}
}
}
16 changes: 16 additions & 0 deletions Interfaces/IEmployeeRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Restaurant_Management.Models;

namespace Restaurant_Management.Interfaces
{
public interface IEmployeeRepository
{
ICollection<Employee> GetEmployees();
Employee? GetEmployee(int id);
ICollection<Employee> GetEmployeesByRestaurant(int restaurantId);
float? GetEmployeeGrossIncome(int id);
int CreateEmployee(Employee employee);
bool DeleteEmployee(Employee employee);
bool UpdateEmployee(Employee employee);
bool Save();
}
}
14 changes: 14 additions & 0 deletions Interfaces/IRestaurantRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Restaurant_Management.Models;

namespace Restaurant_Management.Interfaces
{
public interface IRestaurantRepository
{
ICollection<Restaurant> GetRestaurants();
int CreateRestaurant(Restaurant restaurant);
bool RemoveRestaurant(int id);
Restaurant GetRestaurant(int id);
bool UpdateRestaurant(Restaurant restaurant);
bool Save();
}
}
Loading

0 comments on commit 41b76f5

Please sign in to comment.