forked from MDCPS/DamageAssessment_Backend
Copy from old Repository
This commit is contained in:
@ -0,0 +1,75 @@
|
||||
using DamageAssesment.Api.Surveys.Interfaces;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace DamageAssesment.Api.Surveys.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class SurveysController : ControllerBase
|
||||
{
|
||||
private ISurveyProvider surveyProvider;
|
||||
|
||||
public SurveysController(ISurveyProvider surveyProvider)
|
||||
{
|
||||
this.surveyProvider = surveyProvider;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult> GetSurveysAsync()
|
||||
{
|
||||
var result = await this.surveyProvider.GetSurveysAsync();
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.Surveys);
|
||||
}
|
||||
return NoContent();
|
||||
}
|
||||
[HttpGet("{Id}")]
|
||||
public async Task<ActionResult> GetSurveysAsync(int Id)
|
||||
{
|
||||
var result = await this.surveyProvider.GetSurveysAsync(Id);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.Surveys);
|
||||
}
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ActionResult> PostSurveysAsync(Models.Survey survey)
|
||||
{
|
||||
var result = await this.surveyProvider.PostSurveyAsync(survey);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.Survey);
|
||||
}
|
||||
return BadRequest(result.ErrorMessage);
|
||||
}
|
||||
|
||||
[HttpPut("{Id}")]
|
||||
public async Task<ActionResult> PutSurveysAsync(int Id, Models.Survey survey)
|
||||
{
|
||||
var result = await this.surveyProvider.PutSurveyAsync(Id,survey);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.Survey);
|
||||
}
|
||||
if (result.ErrorMessage == "Not Found")
|
||||
return NotFound(result.ErrorMessage);
|
||||
|
||||
return BadRequest(result.ErrorMessage);
|
||||
}
|
||||
|
||||
[HttpDelete("{Id}")]
|
||||
public async Task<ActionResult> DeleteSurveysAsync(int Id)
|
||||
{
|
||||
var result = await this.surveyProvider.DeleteSurveyAsync(Id);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.Survey);
|
||||
}
|
||||
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>
|
28
DamageAssesmentApi/DamageAssesment.Api.Surveys/Db/Survey.cs
Normal file
28
DamageAssesmentApi/DamageAssesment.Api.Surveys/Db/Survey.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DamageAssesment.Api.Surveys.Db
|
||||
{
|
||||
public class Survey
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
|
||||
[StringLength(100)]
|
||||
[Required]
|
||||
public string Title { get; set; }
|
||||
|
||||
//[StringLength(1000)]
|
||||
//public string Description { get; set; }
|
||||
|
||||
public bool IsEnabled { get; set; }
|
||||
|
||||
public DateTime? StartDate { get; set; }
|
||||
|
||||
public DateTime? EndDate { get; set; }
|
||||
|
||||
//public DateTime CreatedDate { get; set; }
|
||||
|
||||
//[StringLength(6)]
|
||||
//public string EmployeeID { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DamageAssesment.Api.Surveys.Db
|
||||
{
|
||||
public class SurveysDbContext:DbContext
|
||||
{
|
||||
public SurveysDbContext(DbContextOptions options) : base(options)
|
||||
{
|
||||
|
||||
}
|
||||
public DbSet<Db.Survey> Surveys { get; set; }
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
namespace DamageAssesment.Api.Surveys.Interfaces
|
||||
{
|
||||
public interface ISurveyProvider
|
||||
{
|
||||
Task<(bool IsSuccess, IEnumerable< Models.Survey> Surveys, string ErrorMessage)> GetSurveysAsync();
|
||||
Task<(bool IsSuccess, Models.Survey Surveys, string ErrorMessage)> GetSurveysAsync(int Id);
|
||||
Task<(bool IsSuccess, Models.Survey Survey, string ErrorMessage)> PostSurveyAsync(Models.Survey Survey);
|
||||
Task<(bool IsSuccess, Models.Survey Survey, string ErrorMessage)> PutSurveyAsync(int Id,Models.Survey Survey);
|
||||
Task<(bool IsSuccess, Models.Survey Survey, string ErrorMessage)> DeleteSurveyAsync(int Id);
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DamageAssesment.Api.Surveys.Models
|
||||
{
|
||||
public class Survey
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
|
||||
[StringLength(100)]
|
||||
public string Title { get; set; }
|
||||
|
||||
//[StringLength(1000)]
|
||||
// public string? Description { get; set; }
|
||||
|
||||
public bool IsEnabled { get; set; }
|
||||
|
||||
public DateTime? StartDate { get; set; }
|
||||
|
||||
public DateTime? EndDate { get; set; }
|
||||
|
||||
//public DateTime CreatedDate { get; set; }
|
||||
|
||||
//[StringLength(6)]
|
||||
//public string EmployeeID { get; set; }
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
namespace DamageAssesment.Api.Surveys.Profiles
|
||||
{
|
||||
public class SurveysProfile:AutoMapper.Profile
|
||||
{
|
||||
public SurveysProfile() {
|
||||
CreateMap<Db.Survey, Models.Survey>();
|
||||
CreateMap<Models.Survey, Db.Survey>();
|
||||
}
|
||||
}
|
||||
}
|
33
DamageAssesmentApi/DamageAssesment.Api.Surveys/Program.cs
Normal file
33
DamageAssesmentApi/DamageAssesment.Api.Surveys/Program.cs
Normal file
@ -0,0 +1,33 @@
|
||||
using DamageAssesment.Api.Surveys.Db;
|
||||
using DamageAssesment.Api.Surveys.Interfaces;
|
||||
using DamageAssesment.Api.Surveys.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.AddScoped<ISurveyProvider, SurveysProvider>();
|
||||
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddDbContext<SurveysDbContext>(option =>
|
||||
{
|
||||
option.UseInMemoryDatabase("Surveys");
|
||||
});
|
||||
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:51498",
|
||||
"sslPort": 0
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"DamageAssesment.Api.Surveys": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5009",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,160 @@
|
||||
using AutoMapper;
|
||||
using DamageAssesment.Api.Surveys.Db;
|
||||
using DamageAssesment.Api.Surveys.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DamageAssesment.Api.Surveys.Providers
|
||||
{
|
||||
public class SurveysProvider : ISurveyProvider
|
||||
{
|
||||
private readonly SurveysDbContext surveyDbContext;
|
||||
private readonly ILogger<SurveysProvider> logger;
|
||||
private readonly IMapper mapper;
|
||||
|
||||
public SurveysProvider(SurveysDbContext surveysDbContext, ILogger<SurveysProvider> logger, IMapper mapper)
|
||||
{
|
||||
this.surveyDbContext = surveysDbContext;
|
||||
this.logger = logger;
|
||||
this.mapper = mapper;
|
||||
seedData();
|
||||
}
|
||||
|
||||
private void seedData()
|
||||
{
|
||||
if (!surveyDbContext.Surveys.Any())
|
||||
{
|
||||
surveyDbContext.Surveys.Add(new Db.Survey { Id = 1, Title = "Sample Survey Title:Damage Assesment 2014", IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) });
|
||||
surveyDbContext.Surveys.Add(new Db.Survey { Id = 2, Title = "Sample Survey Title: Damage Assesment 2016", IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) });
|
||||
surveyDbContext.Surveys.Add(new Db.Survey { Id = 3, Title = "Sample Survey Title: Damage Assesment 2018", IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) });
|
||||
surveyDbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<(bool IsSuccess, IEnumerable<Models.Survey> Surveys, string ErrorMessage)> GetSurveysAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
logger?.LogInformation("Gell all Surveys from DB");
|
||||
var surveys = await surveyDbContext.Surveys.ToListAsync();
|
||||
if (surveys != null)
|
||||
{
|
||||
logger?.LogInformation($"{surveys.Count} Items(s) found");
|
||||
var result = mapper.Map<IEnumerable<Db.Survey>, IEnumerable<Models.Survey>>(surveys);
|
||||
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.Survey Surveys, string ErrorMessage)> GetSurveysAsync(int Id)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger?.LogInformation("Query Survey");
|
||||
var survey = await surveyDbContext.Surveys.SingleOrDefaultAsync(s => s.Id == Id);
|
||||
if (survey != null)
|
||||
{
|
||||
logger?.LogInformation($"Survey Id: {Id} found");
|
||||
var result = mapper.Map<Db.Survey, Models.Survey>(survey);
|
||||
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.Survey Survey, string ErrorMessage)> PostSurveyAsync(Models.Survey survey)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (survey != null)
|
||||
{
|
||||
var surveys = await surveyDbContext.Surveys.ToListAsync();
|
||||
survey.Id = surveys.Count + 1;
|
||||
surveyDbContext.Surveys.Add(mapper.Map<Models.Survey, Db.Survey>(survey));
|
||||
surveyDbContext.SaveChanges();
|
||||
return (true, survey, "Successful");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger?.LogInformation($"Survey Id: {survey.Id} cannot be added");
|
||||
return (false, null, $"Survey Id: {survey.Id} cannot be added");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<(bool IsSuccess, Models.Survey Survey, string ErrorMessage)> PutSurveyAsync(int Id, Models.Survey survey)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (survey != null)
|
||||
{
|
||||
var _survey = await surveyDbContext.Surveys.Where(s => s.Id == Id).SingleOrDefaultAsync();
|
||||
|
||||
if (_survey != null)
|
||||
{
|
||||
_survey.Title = survey.Title;
|
||||
_survey.IsEnabled = survey.IsEnabled;
|
||||
_survey.StartDate = survey.StartDate;
|
||||
_survey.EndDate = survey.EndDate;
|
||||
surveyDbContext.SaveChanges();
|
||||
return (true, mapper.Map<Db.Survey, Models.Survey>(_survey), "Successful");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger?.LogInformation($"Survey Id : {Id} Not found");
|
||||
return (false, null, "Not Found");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
logger?.LogInformation($"Survey Id: {Id} 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.Survey Survey, string ErrorMessage)> DeleteSurveyAsync(int Id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var survey = await surveyDbContext.Surveys.Where(x => x.Id == Id).SingleOrDefaultAsync();
|
||||
|
||||
if (survey != null)
|
||||
{
|
||||
surveyDbContext.Surveys.Remove(survey);
|
||||
surveyDbContext.SaveChanges();
|
||||
return (true, mapper.Map<Db.Survey, Models.Survey>(survey), $"Survey Id: {Id} deleted Successfuly");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger?.LogInformation($"Survey Id : {Id} Not found");
|
||||
return (false, null, "Not Found");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
Reference in New Issue
Block a user