Skip to content

State province entity #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Almostengr.Common/Almostengr.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.3.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.2" />
<None Include="README.md" Pack="true" PackagePath="\" />
</ItemGroup>
Expand Down
24 changes: 24 additions & 0 deletions Almostengr.Common/Almostengr.Common.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.2.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Almostengr.Common", "Almostengr.Common.csproj", "{291BB587-ED25-875F-8371-60B983B1D40F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{291BB587-ED25-875F-8371-60B983B1D40F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{291BB587-ED25-875F-8371-60B983B1D40F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{291BB587-ED25-875F-8371-60B983B1D40F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{291BB587-ED25-875F-8371-60B983B1D40F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8A092BA3-F6C2-4B9F-AF37-DCA541FC2C11}
EndGlobalSection
EndGlobal
8 changes: 8 additions & 0 deletions Almostengr.Common/Controllers/BaseApiController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Microsoft.AspNetCore.Mvc;

namespace Almostengr.Common.Controllers;

[ApiController]
public abstract class BaseApiController : Controller
{
}
24 changes: 24 additions & 0 deletions Almostengr.Common/Controllers/BaseUiController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Mvc;

namespace Almostengr.Common.Controllers;

public abstract class BaseUiController : Controller
{
protected void AddErrorsToModelState(IReadOnlyList<string> errors)
{
foreach (var error in errors)
{
ModelState.AddModelError(string.Empty, error);
}
}

protected IActionResult NotFoundView()
{
return View("NotFound");
}

protected IActionResult NotAuthorizedView()
{
return View("NotAuthorized");
}
}
15 changes: 9 additions & 6 deletions Almostengr.Common/Domain/BaseEntity.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
using System.ComponentModel.DataAnnotations;
using Almostengr.Common.DomainServices.Results;
using Almostengr.Common.Shared;

namespace Almostengr.Common.Domain;

public abstract class BaseEntity
{
protected BaseEntity() { }

protected BaseEntity(Guid guid, string modifiedBy)
protected BaseEntity(Guid guid, string modifiedBy) : this(guid)
{
Guid = guid;
ModifiedDate = DateTime.UtcNow;
ModifiedBy = modifiedBy;
SetModified(modifiedBy);
}

protected BaseEntity(Guid guid)
{
Guid = guid == Guid.Empty ? Guid.NewGuid() : guid;
}

[Key]
public int Id { get; protected set; }

public Guid Guid { get; protected set; }

[Required, MaxLength(100)]
[Required, MaxLength(LibConstants.MediumLength)]
public string ModifiedBy { get; protected set; }

public DateTime ModifiedDate { get; protected set; }
Expand Down
26 changes: 16 additions & 10 deletions Almostengr.Common/Domain/BaseLookupEntity.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
using System.ComponentModel.DataAnnotations;
using Almostengr.Common.DomainServices.Results;
using Almostengr.Common.Shared;

namespace Almostengr.Common.Domain;

public abstract class BaseLookupEntity : BaseEntity
public abstract class BaseLookupEntity<T> : BaseEntity where T : BaseLookupEntity<T>
{
private BaseLookupEntity(Guid guid, string shortDescription, string fullDescription)
protected BaseLookupEntity(Guid guid, string shortDescription, string fullDescription) : base(guid)
{
ShortDescription = shortDescription;
FullDescription = fullDescription;
}

protected BaseLookupEntity(Guid guid) : base(guid)
{
IsActive = true;
}

protected BaseLookupEntity() : base() { }

[Required, StringLength(100)]
[Required, StringLength(LibConstants.ShortLength)]
public string ShortDescription { get; protected set; }

[StringLength(500)]
[StringLength(LibConstants.LongLength)]
public string FullDescription { get; protected set; }

public bool IsActive { get; protected set; }
public int SortOrder { get; protected set; } = 1;

public abstract Result<BaseLookupEntity> Create(
Guid guid, string shortDescription, string fullDescription, bool isActive, string modifiedBy);
public abstract Result<T> Create(
Guid guid, string shortDescription, string fullDescription, bool isActive, string modifiedBy, int sortOrder = 1);

public Result<BaseLookupEntity> Update(string shortDescription, string fullDescription, bool isActive, string modifiedBy)
public Result<T> Update(string shortDescription, bool isActive, string modifiedBy, int sortOrder = 1, string fullDescription = null)
{
ShortDescription = shortDescription;
FullDescription = fullDescription;
IsActive = isActive;
SortOrder = sortOrder;
FullDescription = fullDescription;
SetModified(modifiedBy);

return Result<BaseLookupEntity>.Success(this);
return Result<T>.Success((T)this);
}
}
16 changes: 16 additions & 0 deletions Almostengr.Common/Domain/Country.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Almostengr.Common.DomainServices.Results;

namespace Almostengr.Common.Domain;

public class Country : BaseLookupEntity<Country>
{
private Country() { }

private Country(Guid guid) : base(guid) { }

public override Result<Country> Create(Guid guid, string shortDescription, string fullDescription, bool isActive, string modifiedBy, int sortOrder = 1)
{
Country country = new(guid);
return country.Update(shortDescription, true, modifiedBy, sortOrder, fullDescription);
}
}
16 changes: 16 additions & 0 deletions Almostengr.Common/Domain/Gender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Almostengr.Common.DomainServices.Results;

namespace Almostengr.Common.Domain;

public class Gender : BaseLookupEntity<Gender>
{
public Gender(Guid guid) : base(guid)
{
}

public override Result<Gender> Create(Guid guid, string shortDescription, string fullDescription, bool isActive, string modifiedBy, int sortOrder = 1)
{
Gender gender = new(guid);
return gender.Update(shortDescription, isActive, modifiedBy, sortOrder, fullDescription);
}
}
8 changes: 8 additions & 0 deletions Almostengr.Common/Domain/GenderEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Almostengr.Common.Domain;

public enum GenderEnum
{
Female = 1,
Male = 2,
Unknown = 3,
}
16 changes: 16 additions & 0 deletions Almostengr.Common/Domain/StateProvince.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Almostengr.Common.DomainServices.Results;

namespace Almostengr.Common.Domain;

public class StateProvince : BaseLookupEntity<StateProvince>
{
private StateProvince() { }

private StateProvince(Guid guid) : base(guid) { }

public override Result<StateProvince> Create(Guid guid, string shortDescription, string fullDescription, bool isActive, string modifiedBy, int sortOrder = 1)
{
StateProvince stateProvince = new(guid);
return stateProvince.Update(shortDescription, true, modifiedBy, sortOrder, fullDescription);
}
}
20 changes: 20 additions & 0 deletions Almostengr.Common/DomainServices/BaseCommandService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Almostengr.Common.DomainServices.Interfaces;
using Almostengr.Common.DomainServices.Resources;
using Almostengr.Common.DomainServices.Results;
using Microsoft.Extensions.Logging;

namespace Almostengr.Common.DomainServices;

public abstract class BaseCommandService<TResource, TService> : ICommandService<TResource>
where TService : class
where TResource : BaseResource
{
protected readonly ILogger<TService> _logger;

protected BaseCommandService(ILogger<TService> logger)
{
_logger = logger;
}

public abstract Task<Result<TResource>> ExecuteAsync(TResource resource);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ public interface IAddRepository<TEntity> : IQueryRepository<TEntity> where TEnti
{
Task AddAsync(TEntity entity);
Task SaveChangesAsync();
Task<Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction> BeginTransactionAsync();
}
5 changes: 3 additions & 2 deletions Almostengr.Common/DomainServices/Interfaces/IAddService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Almostengr.Common.DomainServices.Interfaces;

public interface IAddService<TEntity, TResource> : ICommandService<TResource>
where TResource : BaseResource
[Obsolete]
public interface IAddService<TEntity, TResource> : ICommandService<TResource>
where TResource : BaseResource
where TEntity : BaseEntity;
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace Almostengr.Common.DomainServices.Interfaces;

public interface ICommandService<TResource> where TResource : BaseResource
{
Task<Result<TResource>> ExecuteAsync(TResource resource, bool commitTransaction = true);
Task<Result<TResource>> ExecuteAsync(TResource resource);
}
3 changes: 2 additions & 1 deletion Almostengr.Common/DomainServices/Interfaces/ILookupMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
namespace Almostengr.Common.DomainServices.Interfaces;

public interface ILookupMapper<TEntity, TResource> : IMapper<TEntity, TResource>
where TEntity : BaseLookupEntity where TResource : LookupResource;
where TEntity : BaseLookupEntity<TEntity>
where TResource : LookupResource;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Almostengr.Common.Domain;

namespace Almostengr.Common.DomainServices.Interfaces;

public interface ILookupRepository<TEntity> : IQueryRepository<TEntity> where TEntity : BaseLookupEntity<TEntity>
{
Task<IEnumerable<TEntity>> GetListAsync(bool activeOnly = true);
}
11 changes: 11 additions & 0 deletions Almostengr.Common/DomainServices/Interfaces/ILookupService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Almostengr.Common.Domain;
using Almostengr.Common.DomainServices.Resources;

namespace Almostengr.Common.DomainServices.Interfaces;

public interface ILookupService<TEntity, TResource>
where TEntity : BaseLookupEntity<TEntity>
where TResource : LookupResource
{
Task<IEnumerable<LookupResource>> GetListAsync(bool activeOnly = true);
}

This file was deleted.

This file was deleted.

2 changes: 0 additions & 2 deletions Almostengr.Common/DomainServices/Interfaces/IQueryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ namespace Almostengr.Common.DomainServices.Interfaces;
public interface IQueryService<TEntity, TResource> where TEntity : BaseEntity where TResource : BaseResource
{
Task<IEnumerable<TResource>> GetListAsync();
Task<bool> ExistsByIdAsync(int id);
Task<TResource> GetByIdAsync(int id);
Task<bool> ExistsByGuidAsync(Guid guid);
Task<TResource> GetByGuidAsync(Guid guid);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Almostengr.Common.DomainServices.Resources;
using Almostengr.Common.DomainServices.Results;

namespace Almostengr.Common.DomainServices.Interfaces;

public interface ISyncCommandService<TResource> where TResource : BaseResource
{
Result<TResource> Execute(TResource resource);
}
26 changes: 26 additions & 0 deletions Almostengr.Common/DomainServices/LookupMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Almostengr.Common.Domain;
using Almostengr.Common.DomainServices.Interfaces;
using Almostengr.Common.DomainServices.Resources;

namespace Almostengr.Common.DomainServices;

public abstract class LookupMapper<TEntity> : ILookupMapper<TEntity, LookupResource> where TEntity : BaseLookupEntity<TEntity>
{
public LookupResource ToResource(TEntity entity)
{
if (entity == null)
{
return null;
}

return new LookupResource
{
Guid = entity.Guid,
IsActive = entity.IsActive,
ShortDescription = entity.ShortDescription,
FullDescription = entity.FullDescription,
ModifiedBy = entity.ModifiedBy,
ModifiedDate = entity.ModifiedDate,
};
}
}
26 changes: 26 additions & 0 deletions Almostengr.Common/DomainServices/LookupService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Almostengr.Common.Domain;
using Almostengr.Common.DomainServices.Interfaces;
using Almostengr.Common.DomainServices.Resources;

namespace Almostengr.Common.DomainServices;

public class LookupService<TEntity, TResource> : QueryService<TEntity, TResource>, ILookupService<TEntity, TResource>
where TEntity : BaseLookupEntity<TEntity>
where TResource : LookupResource
{
private readonly ILookupRepository<TEntity> _lookupRepository;

public LookupService(
IMapper<TEntity, TResource> mapper,
ILookupRepository<TEntity> repository
) : base(mapper, repository)
{
_lookupRepository = repository;
}

public async Task<IEnumerable<LookupResource>> GetListAsync(bool activeOnly = true)
{
IEnumerable<TEntity> entities = await _lookupRepository.GetListAsync(activeOnly);
return entities.Select(_mapper.ToResource).ToList();
}
}
Loading