forked from MDCPS/DamageAssessment_Backend
Copy from old Repository
This commit is contained in:
@ -0,0 +1,90 @@
|
||||
using DamageAssesment.Api.Employees.Interfaces;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace DamageAssesment.Api.Employees.Controllers
|
||||
{
|
||||
[Route("api")]
|
||||
[ApiController]
|
||||
public class EmployeesController : ControllerBase
|
||||
{
|
||||
|
||||
private IEmployeesProvider EmployeeProvider;
|
||||
|
||||
public EmployeesController(IEmployeesProvider EmployeesProvider)
|
||||
{
|
||||
this.EmployeeProvider = EmployeesProvider;
|
||||
}
|
||||
//get all Employees
|
||||
[HttpGet("Employees")]
|
||||
public async Task<ActionResult> GetEmployeesAsync()
|
||||
{
|
||||
|
||||
var result = await EmployeeProvider.GetEmployeesAsync();
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.Employees);
|
||||
}
|
||||
return NoContent();
|
||||
|
||||
}
|
||||
//get Employee based on Employeeid
|
||||
[HttpGet("Employees/{Id}")]
|
||||
public async Task<ActionResult> GetEmployeeByIdAsync(string Id)
|
||||
{
|
||||
|
||||
var result = await EmployeeProvider.GetEmployeeByIdAsync(Id);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.Employee);
|
||||
}
|
||||
return NotFound();
|
||||
|
||||
}
|
||||
//update existing Employee
|
||||
|
||||
[HttpPut("Employees")]
|
||||
public async Task<IActionResult> UpdateEmployee(Db.Employee Employee)
|
||||
{
|
||||
if (Employee != null)
|
||||
{
|
||||
var result = await this.EmployeeProvider.UpdateEmployeeAsync(Employee);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.Employee);
|
||||
}
|
||||
if (result.ErrorMessage == "Not Found")
|
||||
return NotFound(result.ErrorMessage);
|
||||
|
||||
return BadRequest(result.ErrorMessage);
|
||||
}
|
||||
return NotFound();
|
||||
}
|
||||
//save new Employee
|
||||
[HttpPost("Employees")]
|
||||
public async Task<IActionResult> CreateEmployee(Db.Employee Employee)
|
||||
{
|
||||
if (Employee != null)
|
||||
{
|
||||
var result = await this.EmployeeProvider.PostEmployeeAsync(Employee);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.Employee);
|
||||
}
|
||||
return BadRequest(result.ErrorMessage);
|
||||
}
|
||||
return CreatedAtRoute("DefaultApi", new { id = Employee.Id }, Employee);
|
||||
}
|
||||
//delete existing Employee
|
||||
[HttpDelete("Employees/{id}")]
|
||||
public async Task<IActionResult> DeleteEmployee(string id)
|
||||
{
|
||||
var result = await this.EmployeeProvider.DeleteEmployeeAsync(id);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.Employee);
|
||||
}
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.5" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,23 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DamageAssesment.Api.Employees.Db
|
||||
{
|
||||
public class Employee
|
||||
{
|
||||
[Key]
|
||||
public string Id { get; set; }
|
||||
|
||||
[StringLength(50)]
|
||||
public string Name { get; set; }
|
||||
|
||||
public DateTime BirthDate { get; set; }
|
||||
|
||||
[StringLength(50)]
|
||||
public string OfficePhoneNumber { get; set; }
|
||||
|
||||
[StringLength(50)]
|
||||
public string Email { get; set; }
|
||||
public bool IsActive {get;set;}
|
||||
public string? PreferredLanguage { get; set; } = "en";
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DamageAssesment.Api.Employees.Db
|
||||
{
|
||||
public class EmployeeDbContext: DbContext
|
||||
{
|
||||
public DbSet<Db.Employee> Employees { get; set; }
|
||||
public EmployeeDbContext(DbContextOptions options) : base(options)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
namespace DamageAssesment.Api.Employees.Interfaces
|
||||
{
|
||||
public interface IEmployeesProvider
|
||||
{
|
||||
Task<(bool IsSuccess, IEnumerable<Models.Employee> Employees, string ErrorMessage)> GetEmployeesAsync();
|
||||
Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> GetEmployeeByIdAsync(string Id);
|
||||
Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> PostEmployeeAsync(Db.Employee Employee);
|
||||
Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> UpdateEmployeeAsync(Db.Employee Employee);
|
||||
Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> DeleteEmployeeAsync(string Id);
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DamageAssesment.Api.Employees.Models
|
||||
{
|
||||
public class Employee
|
||||
{
|
||||
[Key]
|
||||
public string Id { get; set; }
|
||||
|
||||
[StringLength(50)]
|
||||
public string Name { get; set; }
|
||||
|
||||
public DateTime BirthDate { get; set; }
|
||||
|
||||
[StringLength(50)]
|
||||
public string OfficePhoneNumber { get; set; }
|
||||
|
||||
[StringLength(50)]
|
||||
public string Email { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
public string? PreferredLanguage { get; set; } = "en";
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
using AutoMapper;
|
||||
|
||||
namespace DamageAssesment.Api.Employees.Profiles
|
||||
{
|
||||
public class EmployeesProfile:AutoMapper.Profile
|
||||
{
|
||||
public EmployeesProfile()
|
||||
{
|
||||
CreateMap<Db.Employee, Models.Employee>();
|
||||
}
|
||||
}
|
||||
}
|
34
DamageAssesmentApi/DamageAssesment.Api.Employees/Program.cs
Normal file
34
DamageAssesmentApi/DamageAssesment.Api.Employees/Program.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using DamageAssesment.Api.Employees.Db;
|
||||
using DamageAssesment.Api.Employees.Interfaces;
|
||||
using DamageAssesment.Api.Employees.Providers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddScoped<IEmployeesProvider, EmployeesProvider>();
|
||||
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); //4/30
|
||||
builder.Services.AddDbContext<EmployeeDbContext>(option =>
|
||||
{
|
||||
option.UseInMemoryDatabase("Employees");
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
@ -0,0 +1,31 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:14425",
|
||||
"sslPort": 0
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"DamageAssesment.Api.Employees": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5135",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,164 @@
|
||||
using AutoMapper;
|
||||
using DamageAssesment.Api.Employees.Db;
|
||||
using DamageAssesment.Api.Employees.Interfaces;
|
||||
using DamageAssesment.Api.Employees.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace DamageAssesment.Api.Employees.Providers
|
||||
{
|
||||
public class EmployeesProvider : IEmployeesProvider
|
||||
{
|
||||
|
||||
private EmployeeDbContext EmployeeDbContext;
|
||||
private ILogger<EmployeesProvider> logger;
|
||||
private IMapper mapper;
|
||||
|
||||
public EmployeesProvider(EmployeeDbContext EmployeeDbContext, ILogger<EmployeesProvider> logger, IMapper mapper)
|
||||
{
|
||||
this.EmployeeDbContext = EmployeeDbContext;
|
||||
this.logger = logger;
|
||||
this.mapper = mapper;
|
||||
SeedData();
|
||||
}
|
||||
|
||||
public async Task<(bool IsSuccess, IEnumerable<Models.Employee> Employees, string ErrorMessage)> GetEmployeesAsync()
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
logger?.LogInformation("Query Question");
|
||||
var Employee = await EmployeeDbContext.Employees.AsNoTracking().ToListAsync();
|
||||
if (Employee != null)
|
||||
{
|
||||
logger?.LogInformation($"{Employee.Count} Employees(s) found");
|
||||
var result = mapper.Map<IEnumerable<Db.Employee>, IEnumerable<Models.Employee>>(Employee);
|
||||
return (true, result, null);
|
||||
}
|
||||
return (false, null, "Not found");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> GetEmployeeByIdAsync(string Id)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger?.LogInformation("Query Employee");
|
||||
var Employee = await EmployeeDbContext.Employees.AsNoTracking().FirstOrDefaultAsync(q => q.Id.ToLower() == Id.ToLower());
|
||||
if (Employee != null)
|
||||
{
|
||||
logger?.LogInformation($"{Employee} customer(s) found");
|
||||
var result = mapper.Map<Db.Employee, Models.Employee>(Employee);
|
||||
return (true, result, null);
|
||||
}
|
||||
return (false, null, "Not found");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
public async Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> PostEmployeeAsync(Db.Employee Employee)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger?.LogInformation("Query Employee");
|
||||
if (!EmployeeExists(Employee.Id))
|
||||
{
|
||||
EmployeeDbContext.Employees.Add(Employee);
|
||||
EmployeeDbContext.SaveChanges();
|
||||
var result = mapper.Map<Db.Employee, Models.Employee>(Employee);
|
||||
return (true, result, null);
|
||||
}
|
||||
return (false, null, "Employee is already exits");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
public async Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> UpdateEmployeeAsync(Db.Employee Employee)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Employee != null)
|
||||
{
|
||||
var _employee = await EmployeeDbContext.Employees.AsNoTracking().Where(s => s.Id.ToLower() == Employee.Id.ToLower()).FirstOrDefaultAsync();
|
||||
|
||||
if (_employee != null)
|
||||
{
|
||||
EmployeeDbContext.Employees.Update(Employee);
|
||||
EmployeeDbContext.SaveChanges();
|
||||
return (true, mapper.Map<Db.Employee, Models.Employee>(Employee), "Successful");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger?.LogInformation($"{Employee} Not found");
|
||||
return (false, null, "Not Found");
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
logger?.LogInformation($"{Employee} Bad Request");
|
||||
return (false, null, "Bad request");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
public async Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> DeleteEmployeeAsync(string Id)
|
||||
{
|
||||
try
|
||||
{
|
||||
Db.Employee Employee = EmployeeDbContext.Employees.AsNoTracking().Where(a => a.Id.ToLower() == Id.ToLower()).FirstOrDefault();
|
||||
if (Employee == null)
|
||||
{
|
||||
return (false, null, "Not Found");
|
||||
}
|
||||
EmployeeDbContext.Employees.Remove(Employee);
|
||||
EmployeeDbContext.SaveChanges();
|
||||
return (true, mapper.Map<Db.Employee, Models.Employee>(Employee), $"EmployeeId {Id} deleted Successfuly");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false,null, ex.Message);
|
||||
}
|
||||
}
|
||||
private bool EmployeeExists(string id)
|
||||
{
|
||||
return EmployeeDbContext.Employees.AsNoTracking().Count(e => e.Id.ToLower() == id.ToLower()) > 0;
|
||||
}
|
||||
|
||||
private void SeedData()
|
||||
{
|
||||
if (!EmployeeDbContext.Employees.Any())
|
||||
{
|
||||
EmployeeDbContext.Employees.Add(new Db.Employee() { Id = "Emp1", Name = "ABC1", Email = "abc1@gmail.com", OfficePhoneNumber = "12345678",BirthDate=DateTime.Now.AddYears(-18), IsActive = true,PreferredLanguage="en" });
|
||||
EmployeeDbContext.Employees.Add(new Db.Employee() { Id = "Emp2", Name = "ABC2", Email = "abc2@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-22), IsActive = true, PreferredLanguage = "fr" });
|
||||
EmployeeDbContext.Employees.Add(new Db.Employee() { Id = "Emp3", Name = "ABC3", Email = "abc3@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-30) ,IsActive = true, PreferredLanguage = "fr" });
|
||||
EmployeeDbContext.Employees.Add(new Db.Employee() { Id = "Emp4", Name = "ABC4", Email = "abc4@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-20) ,IsActive = true, PreferredLanguage = "en" });
|
||||
EmployeeDbContext.Employees.Add(new Db.Employee() { Id = "Emp5", Name = "ABC5", Email = "abc5@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-23) ,IsActive = true, PreferredLanguage = "sp" });
|
||||
EmployeeDbContext.Employees.Add(new Db.Employee() { Id = "Emp6", Name = "ABC6", Email = "abc6@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-32) ,IsActive = true, PreferredLanguage = "sp" });
|
||||
EmployeeDbContext.SaveChanges();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"settings": {
|
||||
"endpoint1": "xxx",
|
||||
"endpoint2": "xxx",
|
||||
"endpoint3": "xxx"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user