Skip to content

Commit

Permalink
add: added customer setup (CRU)
Browse files Browse the repository at this point in the history
  • Loading branch information
thuraphyo committed Feb 8, 2024
1 parent a03b112 commit 49bef3e
Show file tree
Hide file tree
Showing 277 changed files with 85,756 additions and 1 deletion.
1 change: 1 addition & 0 deletions ATM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.32" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.32" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.32" />
Expand Down
57 changes: 57 additions & 0 deletions AppServices/Authentication/AuthenticationAppService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using ATM.Areas.Identity.Data;
using Microsoft.AspNetCore.Identity;
using System.Threading.Tasks;
using System;
using ATM.AppServices.Authentication.Dtos;
using Microsoft.Extensions.DependencyInjection;

namespace ATM.AppServices.Authentication
{
public class AuthenticationAppService : IAuthenticationAppService
{
private readonly UserManager<ApplicationUser> _userManager;

public AuthenticationAppService(UserManager<ApplicationUser> userManager)
{
_userManager = userManager;
}

public async Task<string> CreateUser(CreateApplicationUserDto input)
{
var user = await _userManager.FindByNameAsync(input.UserName);
if (user == null)
{
user = new ApplicationUser
{
UserName = input.UserName,
Email = input.UserName,
IsActive = true,
UserType = input.UserType,
EmailConfirmed = true,
};
await _userManager.CreateAsync(user, input.Password);
}

if (user == null)
{
throw new Exception("The password is probably not strong enough!");
}

return user.Id;
}

public async Task<IdentityResult> AssignRoleToUser(string uid, string role)
{
IdentityResult IR;
var user = await _userManager.FindByIdAsync(uid);

if (user == null)
{
throw new Exception("The user password was probably not strong enough!");
}

IR = await _userManager.AddToRoleAsync(user, role);
return IR;
}
}
}
41 changes: 41 additions & 0 deletions AppServices/Authentication/Dtos/ApplicationUserDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using ATM.Helpers;
using Microsoft.AspNetCore.Identity;
using System.ComponentModel.DataAnnotations;
using System;

namespace ATM.AppServices.Authentication.Dtos
{
public class ApplicationUserDto : IdentityUser
{
public int UserType { get; set; }
public bool IsActive { get; set; }

#region Audit Log Info
[StringLength(MaxLength.Max_450)]
public string CreatedUserId { get; set; }
public DateTime? CreationTime { get; set; }

[StringLength(MaxLength.Max_450)]
public string UpdatedUserId { get; set; }
public DateTime? UpdatedTime { get; set; }
#endregion
}

public class CreateApplicationUserDto : IdentityUser
{
public string Password { get; set; }
public int UserType { get; set; }
public bool IsActive { get; set; } = true;

#region Audit Log Info
[StringLength(MaxLength.Max_450)]
public string CreatedUserId { get; set; }
public DateTime? CreationTime { get; set; } = DateTime.Now;
#endregion

public CreateApplicationUserDto()
{

}
}
}
10 changes: 10 additions & 0 deletions AppServices/Authentication/IAuthenticationAppService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using ATM.AppServices.Authentication.Dtos;
using System.Threading.Tasks;

namespace ATM.AppServices.Authentication
{
public interface IAuthenticationAppService
{
Task<string> CreateUser(CreateApplicationUserDto input);
}
}
115 changes: 115 additions & 0 deletions AppServices/CustomerSetup/CustomerAppService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using ATM.AppServices.CustomerSetup.Dtos;
using ATM.Data;
using ATM.Helpers;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using static Microsoft.Extensions.Logging.EventSource.LoggingEventSource;

namespace ATM.AppServices.CustomerSetup
{
public class CustomerAppService : ICustomerAppService
{
private readonly ATMContext _context;
private readonly IMapper _mapper;

public CustomerAppService(ATMContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}

#region Get
public async Task<CustomerDto> GetDetailById(int id)
{
var customer = await _context.Customers.FindAsync(id);
if (customer == null)
{
return new CustomerDto();
}
var existingObj = await _context.Customers.AsNoTracking().FirstOrDefaultAsync(x => x.CustomerId == id);
return _mapper.Map<CustomerDto>(existingObj);
}

public async Task<CustomerDto> GetDetailByGuid(string guid)
{
if (!await _context.Customers.AnyAsync(x => x.CustomerGuid.ToString() == guid))
{
return new CustomerDto();
}
var existingObj = await _context.Customers.AsNoTracking().FirstOrDefaultAsync(x => x.CustomerGuid.ToString() == guid);
return _mapper.Map<CustomerDto>(existingObj);
}

public List<CustomerDto> GetAll(SearchCustomerDto input)
{
var predicate = PredicateBuilder.True<Customer>();

if (!string.IsNullOrEmpty(input.NRIC))
predicate = predicate.And(p => p.NRIC.Contains(input.NRIC));

var objs = _context.Customers
.AsNoTracking()
.Where(predicate)
.Include(x => x.CreatedUser)
.Include(x => x.UpdatedUser)
.OrderBy(x => x.FirstName)
.AsQueryable();

return _mapper.Map<List<CustomerDto>>(objs);
}
#endregion

#region Create
public async Task<CustomerDto> CreateCustomer(CreateCustomerDto input)
{
if (input == null)
{
return new CustomerDto();
}
var obj = _mapper.Map<Customer>(input);
await _context.Customers.AddAsync(obj);
_context.SaveChanges();
return _mapper.Map<CustomerDto>(obj);
}

public async Task<string> CheckDuplicateOnCreate(string nric)
{
if (await _context.Customers.AnyAsync(x => x.NRIC == nric))
return SCustomerMessage.DuplicatedNRIC;
else
return string.Empty;
}
#endregion

#region Update
public async Task<CustomerDto> UpdateCustomer(UpdateCustomerDto input)
{
if (!await _context.Customers.AnyAsync(x => x.CustomerId == input.CustomerId))
{
return new CustomerDto();
}

var existingObj = await _context.Customers.FirstOrDefaultAsync(x => x.CustomerId == input.CustomerId);
_mapper.Map(input, existingObj);
_context.Customers.Update(existingObj);
_context.SaveChanges();
return _mapper.Map<CustomerDto>(existingObj);
}

public async Task<string> CheckDuplicateOnUpdate(int id, string nric)
{
if (await _context.Customers.AnyAsync(x => x.CustomerId != id && x.NRIC == nric))
return SCustomerMessage.DuplicatedNRIC;
else
return string.Empty;
}
#endregion
}
}
150 changes: 150 additions & 0 deletions AppServices/CustomerSetup/Dtos/CustomerDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
using ATM.Areas.Identity.Data;
using ATM.Data;
using ATM.Helpers;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System;
using ATM.AppServices.Authentication;

namespace ATM.AppServices.CustomerSetup.Dtos
{
public class CustomerDto : AuditInfo
{
public int CustomerId { get; set; }
public Guid CustomerGuid { get; set; }

[Required]
[Display(Name = "First Name")]
[StringLength(MaxLength.Max_100)]
public string FirstName { get; set; }

[Required]
[Display(Name = "Date Of Birth")]
public DateTime DateOfBirth { get; set; }

[Required]
[Display(Name = "NRIC")]
[StringLength(MaxLength.Max_50)]
public string NRIC { get; set; }

[Display(Name = "Father Name")]
[StringLength(MaxLength.Max_100)]
public string FatherName { get; set; }

[EmailAddress]
[Display(Name = "Email Address")]
[StringLength(MaxLength.Max_100)]
public string Email { get; set; }

[Required]
[Display(Name = "Mobile Number")]
[StringLength(MaxLength.Max_12)]
public string MobileNumber { get; set; }

[Display(Name = "Job Title")]
[StringLength(MaxLength.Max_100)]
public string JobTitle { get; set; }

[Display(Name = "Address")]
[StringLength(MaxLength.Max_1000)]
public string Address { get; set; }

[Display(Name = "Remarks")]
[StringLength(MaxLength.Max_500)]
public string Remark { get; set; }

[Display(Name = "Enables?")]
public bool IsActive { get; set; }
}

public class CreateCustomerDto : CreatedUser
{
[Required]
[Display(Name = "First Name")]
[StringLength(MaxLength.Max_100)]
public string FirstName { get; set; }

[Required]
[Display(Name = "Last Name")]
[StringLength(MaxLength.Max_100)]
public string LastName { get; set; }

[Required]
[Display(Name = "Date Of Birth")]
public DateTime DateOfBirth { get; set; }

[Required]
[Display(Name = "NRIC")]
[StringLength(MaxLength.Max_50)]
public string NRIC { get; set; }

[Display(Name = "Father Name")]
[StringLength(MaxLength.Max_100)]
public string FatherName { get; set; }

[Required]
[Display(Name = "Mobile Number")]
[StringLength(MaxLength.Max_12)]
public string MobileNumber { get; set; }

[EmailAddress]
[Display(Name = "Email Address")]
[StringLength(MaxLength.Max_100)]
public string Email { get; set; }

[Display(Name = "Job Title")]
[StringLength(MaxLength.Max_100)]
public string JobTitle { get; set; }

[Display(Name = "Address")]
[StringLength(MaxLength.Max_1000)]
public string Address { get; set; }

[Display(Name = "Remarks")]
[StringLength(MaxLength.Max_500)]
public string Remark { get; set; }

public bool IsActive { get; set; } = true;
}

public class UpdateCustomerDto : UpdatedUser
{
public int CustomerId { get; set; }
public Guid CustomerGuid { get; set; }

[StringLength(MaxLength.Max_100)]
public string FirstName { get; set; }

[StringLength(MaxLength.Max_100)]
public string LastName { get; set; }

public DateTime DateOfBirth { get; set; }

[StringLength(MaxLength.Max_50)]
public string NRIC { get; set; }

[StringLength(MaxLength.Max_100)]
public string FatherName { get; set; }

[StringLength(MaxLength.Max_100)]
public string Email { get; set; }

[StringLength(MaxLength.Max_100)]
public string JobTitle { get; set; }

[StringLength(MaxLength.Max_1000)]
public string Address { get; set; }

[StringLength(MaxLength.Max_500)]
public string Remark { get; set; }
}

public class SearchCustomerDto
{
public string CustomerName { get; set; } = string.Empty;
public DateTime? DateOfBirth { get; set; } = null;
public string NRIC { get; set; } = string.Empty;
public string FatherName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
}
}
Loading

0 comments on commit 49bef3e

Please sign in to comment.