188 lines
4.4 KiB
C#
188 lines
4.4 KiB
C#
using CarParkValidationAPI.Datas;
|
|
using CarParkValidationAPI.Entities;
|
|
using CarParkValidationAPI.Interface;
|
|
using CarParkValidationAPI.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Options;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CarParkValidationAPI.Repository
|
|
{
|
|
public class ValidationPointRepository : IValidationPoint
|
|
{
|
|
private DataContext _context;
|
|
|
|
private readonly AppSettings _appSettings;
|
|
private string _currentUser = "";
|
|
private List<ValidationPointDto> _list = new List<ValidationPointDto>();
|
|
|
|
public ValidationPointRepository(
|
|
DataContext context,
|
|
|
|
IOptions<AppSettings> appSettings)
|
|
{
|
|
_context = context;
|
|
_appSettings = appSettings.Value;
|
|
}
|
|
public void SetCurrentUser(string username)
|
|
{
|
|
_currentUser = username;
|
|
}
|
|
private ValidationPointDto FillValidationDto(ValidationPoint item)
|
|
{
|
|
ValidationPointDto dto = new ValidationPointDto();
|
|
dto.Active = item.Active;
|
|
dto.Description = item.Description;
|
|
dto.Display = item.DisplayName;
|
|
dto.Id = item.Id;
|
|
return dto;
|
|
}
|
|
|
|
private ValidationPoint FillValidationModel(ValidationPointDto item, ValidationPoint model) {
|
|
|
|
model.Active = item.Active;
|
|
model.Description = item.Description.Trim();
|
|
if (!string.IsNullOrEmpty(item.Display))
|
|
model.DisplayName = item.Display.Trim();
|
|
|
|
return model;
|
|
}
|
|
|
|
public async Task<ResultModel<List<ValidationPointDto>>> LoadValidationPointAsync(bool onlyActive)
|
|
{
|
|
int statuscode = 0;
|
|
string error = "";
|
|
try
|
|
{
|
|
var it = await _context.Validations.ToListAsync();
|
|
|
|
ValidationPointDto dto;
|
|
for (int i = 0; i < it.Count; i++)
|
|
{
|
|
dto = FillValidationDto(it[i]);
|
|
_list.Add(dto);
|
|
}
|
|
_list.Sort((x, y) => x.Description.CompareTo(y.Description));
|
|
statuscode = 1;
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
error = ex.ToString();
|
|
statuscode = -1;
|
|
|
|
}
|
|
return new ResultModel<List<ValidationPointDto>>()
|
|
{
|
|
Data = _list,
|
|
StatusCode = statuscode,
|
|
Message = error
|
|
};
|
|
}
|
|
public async Task<ResultModel<ValidationPointDto>> LoadValidationPointByIdAsync(int id)
|
|
{
|
|
ValidationPoint result = null;
|
|
int statuscode = 0;
|
|
string error = "";
|
|
ValidationPointDto dto = null;
|
|
try
|
|
{
|
|
result = await _context.Validations.FindAsync(id);
|
|
dto = FillValidationDto(result);
|
|
//result = _list.Find(x => x.Id == id);
|
|
//var dto = await Task.Run(() => result);
|
|
statuscode = 1;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
error = ex.ToString();
|
|
statuscode = -1;
|
|
}
|
|
return new ResultModel<ValidationPointDto>()
|
|
{
|
|
Data = dto,
|
|
StatusCode = statuscode,
|
|
Message = error
|
|
};
|
|
}
|
|
public async Task<ResultModel<int>> SaveValidationPointAsync(ValidationPointDto item)
|
|
{
|
|
int result = default(int);
|
|
int statuscode = 0;
|
|
string error = "";
|
|
try
|
|
{
|
|
ValidationPoint model;
|
|
if (item.Id > 0)
|
|
{
|
|
model = await _context.Validations.FindAsync(item.Id);
|
|
model = FillValidationModel(item, model);
|
|
model.LastModified = DateTime.Now;
|
|
model.LastModifiedId = _currentUser;
|
|
await _context.SaveChangesAsync();
|
|
result = item.Id;
|
|
}
|
|
else //new record
|
|
{
|
|
model = new ValidationPoint();
|
|
model.LastModified = DateTime.Now;
|
|
model.LastModifiedId = _currentUser;
|
|
model = FillValidationModel(item, model);
|
|
_context.Validations.Add(model);
|
|
var id = await _context.SaveChangesAsync();
|
|
result = model.Id;
|
|
|
|
}
|
|
statuscode = 1;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
error = ex.ToString();
|
|
statuscode = -1;
|
|
}
|
|
//var dto = await Task.Run(() => result);
|
|
return new ResultModel<int>()
|
|
{
|
|
Data = result,
|
|
StatusCode = statuscode,
|
|
Message = error
|
|
};
|
|
}
|
|
|
|
public async Task<ResultModel<bool>> DeleteValidationPointAsync(int id)
|
|
{
|
|
bool result = true;
|
|
int statuscode = 0;
|
|
string error = "";
|
|
try
|
|
{
|
|
ValidationPoint model;
|
|
if (id > 0)
|
|
{
|
|
model = await _context.Validations.FindAsync(id);
|
|
model.Active = false;
|
|
//_context.Validations.Remove(model);
|
|
await _context.SaveChangesAsync();
|
|
statuscode = 1;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
error = ex.ToString();
|
|
statuscode = -1;
|
|
|
|
}
|
|
//var dto = await Task.Run(() => result);
|
|
return new ResultModel<bool>()
|
|
{
|
|
Data = result,
|
|
StatusCode = statuscode,
|
|
Message = error
|
|
};
|
|
}
|
|
}
|
|
}
|