added a notes file for important running stuff
Create working for tenant and added in efcore and data layer
This commit is contained in:
@@ -1,29 +1,73 @@
|
||||
using Emergence.models;
|
||||
using Emergence.data.Contexts;
|
||||
using Emergence.data.Models;
|
||||
using Emergence.models;
|
||||
using Emergence.services.Interface;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Text;
|
||||
|
||||
namespace Emergence.services.Services;
|
||||
|
||||
public static class TenantExtension
|
||||
{
|
||||
public static TenantModel ConvertToTenantModel(this Tenant tenant)
|
||||
{
|
||||
TenantModel model = new TenantModel
|
||||
{
|
||||
Id = tenant.Id,
|
||||
TenantCode = tenant.TenantCode,
|
||||
CompanyName = tenant.CompanyName,
|
||||
IsInactive = tenant.IsInactive,
|
||||
};
|
||||
return model;
|
||||
}
|
||||
public static Tenant ConvertToTenant(this TenantModel tenant)
|
||||
{
|
||||
Tenant model = new Tenant
|
||||
{
|
||||
Id = tenant.Id.HasValue ? tenant.Id.Value : Guid.NewGuid(),
|
||||
TenantCode = tenant.TenantCode,
|
||||
CompanyName = tenant.CompanyName,
|
||||
IsInactive = tenant.IsInactive,
|
||||
};
|
||||
return model;
|
||||
}
|
||||
}
|
||||
public class TenantService : ITenantService
|
||||
{
|
||||
private List<TenantModel> testList = [
|
||||
new TenantModel {Id = Guid.NewGuid(), TenantCode = "FJP", CompanyName = "Fred J Potter", IsInactive = false },
|
||||
new TenantModel {Id = Guid.NewGuid(), TenantCode = "JAZZ", CompanyName = "Jazzbond", IsInactive = false },
|
||||
new TenantModel {Id = Guid.NewGuid(), TenantCode = "TDT", CompanyName = "The Digital Tailor", IsInactive = false },
|
||||
new TenantModel {Id = Guid.NewGuid(), TenantCode = "HBAS", CompanyName = "Hicks Building and Asbestos Services", IsInactive = false },
|
||||
];
|
||||
private readonly AdminDbContext _dbContext;
|
||||
public TenantService(AdminDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TenantModel>> GetAllAsync()
|
||||
{
|
||||
return testList;
|
||||
return _dbContext.Tenant.Select(s => s.ConvertToTenantModel());
|
||||
}
|
||||
|
||||
public async Task<TenantModel> GetByIdAsync(Guid id)
|
||||
{
|
||||
#pragma warning disable CS8603 // Possible null reference return.
|
||||
return testList.FirstOrDefault(f => f.Id == id);
|
||||
#pragma warning restore CS8603 // Possible null reference return.
|
||||
var entity = _dbContext.Tenant.Where(f => f.Id == id)?.Select(s => s.ConvertToTenantModel()).FirstOrDefault();
|
||||
return entity;
|
||||
}
|
||||
|
||||
public async Task<TenantModel> CreateAsync(TenantModel model)
|
||||
{
|
||||
if (model.Id == Guid.Empty || model.Id == null)
|
||||
model.Id = Guid.NewGuid();
|
||||
|
||||
Tenant newTenant = model.ConvertToTenant();
|
||||
try
|
||||
{
|
||||
_dbContext.Tenant.Add(newTenant);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
return model;
|
||||
}
|
||||
catch (DbException ex)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user