mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 11:53:12 +00:00
145 lines
5.1 KiB
C#
145 lines
5.1 KiB
C#
using Data.SeaHavenIndustries;
|
|
using FluentValidation;
|
|
using SeaHaven.DataServices.Interfaces;
|
|
using SeaHaven.Services.DTOs;
|
|
using SeaHaven.Services.Interfaces;
|
|
using SeaHaven.Services.Validation;
|
|
|
|
namespace SeaHaven.Services.Implementation
|
|
{
|
|
/// <summary>
|
|
/// Account service implementation
|
|
/// Contains business logic, validation, and orchestration for Account operations
|
|
/// </summary>
|
|
public class AccountService : IAccountService
|
|
{
|
|
private readonly IAccountDataService _accountDataService;
|
|
private readonly ICreateAccountValidation _createValidator;
|
|
private readonly IUpdateAccountValidation _updateValidator;
|
|
|
|
public AccountService(
|
|
IAccountDataService accountDataService,
|
|
ICreateAccountValidation createValidator,
|
|
IUpdateAccountValidation updateValidator)
|
|
{
|
|
_accountDataService = accountDataService;
|
|
_createValidator = createValidator;
|
|
_updateValidator = updateValidator;
|
|
}
|
|
|
|
public async Task<AccountDTO?> GetAccountByIdAsync(int id)
|
|
{
|
|
var account = await _accountDataService.GetByIdAsync(id);
|
|
return account == null ? null : MapToDTO(account);
|
|
}
|
|
|
|
public async Task<AccountDTO?> GetAccountByIdWithDetailsAsync(int id)
|
|
{
|
|
var account = await _accountDataService.GetByIdWithDetailsAsync(id);
|
|
return account == null ? null : MapToDTO(account);
|
|
}
|
|
|
|
public async Task<IEnumerable<AccountDTO>> GetAllAccountsAsync()
|
|
{
|
|
var accounts = await _accountDataService.GetAllAsync();
|
|
return accounts.Select(MapToDTO);
|
|
}
|
|
|
|
public async Task<PagedResult<AccountDTO>> GetAccountsPagedAsync(int page, int pageSize, string? search = null)
|
|
{
|
|
var (items, totalCount) = await _accountDataService.GetPagedAsync(page, pageSize, search);
|
|
return new PagedResult<AccountDTO>
|
|
{
|
|
Items = items.Select(MapToDTO).ToList(),
|
|
TotalCount = totalCount,
|
|
Page = page,
|
|
PageSize = pageSize
|
|
};
|
|
}
|
|
|
|
public async Task<AccountDTO> CreateAccountAsync(CreateAccountDTO dto, string userId)
|
|
{
|
|
var validationResult = await _createValidator.ValidateAsync(dto);
|
|
if (!validationResult.IsValid)
|
|
{
|
|
throw new ValidationException(validationResult.Errors);
|
|
}
|
|
|
|
var account = new Accounts
|
|
{
|
|
Name = dto.Name,
|
|
Phone = dto.Phone,
|
|
Email = dto.Email,
|
|
Website = dto.Website,
|
|
CreatedDate = DateTime.UtcNow,
|
|
createdby = userId
|
|
};
|
|
|
|
var created = await _accountDataService.AddAsync(account);
|
|
return MapToDTO(created);
|
|
}
|
|
|
|
public async Task<AccountDTO> UpdateAccountAsync(int id, UpdateAccountDTO dto, string userId)
|
|
{
|
|
var validationResult = await _updateValidator.ValidateAsync(dto);
|
|
if (!validationResult.IsValid)
|
|
{
|
|
throw new ValidationException(validationResult.Errors);
|
|
}
|
|
|
|
var account = await _accountDataService.GetByIdAsync(id);
|
|
if (account == null)
|
|
throw new InvalidOperationException($"Account with ID {id} not found");
|
|
|
|
if (dto.Name != null) account.Name = dto.Name;
|
|
if (dto.Phone != null) account.Phone = dto.Phone;
|
|
if (dto.Email != null) account.Email = dto.Email;
|
|
if (dto.Website != null) account.Website = dto.Website;
|
|
|
|
account.LastModificationTime = DateTime.UtcNow;
|
|
if (int.TryParse(userId, out int userIdInt))
|
|
account.LastModifierUserId = userIdInt;
|
|
|
|
await _accountDataService.UpdateAsync(account);
|
|
return MapToDTO(account);
|
|
}
|
|
|
|
public async Task DeleteAccountAsync(int id, string userId)
|
|
{
|
|
var account = await _accountDataService.GetByIdAsync(id);
|
|
if (account == null)
|
|
throw new InvalidOperationException($"Account with ID {id} not found");
|
|
|
|
account.IsDeleted = true;
|
|
account.DeleterUserId = userId;
|
|
account.DeletionTime = DateTime.UtcNow;
|
|
|
|
await _accountDataService.UpdateAsync(account);
|
|
}
|
|
|
|
public async Task<bool> AccountExistsAsync(int id)
|
|
{
|
|
return await _accountDataService.ExistsAsync(id);
|
|
}
|
|
|
|
public async Task<int> GetTotalAccountCountAsync()
|
|
{
|
|
return await _accountDataService.CountAsync();
|
|
}
|
|
|
|
private AccountDTO MapToDTO(Accounts account)
|
|
{
|
|
return new AccountDTO
|
|
{
|
|
Id = account.Id,
|
|
Name = account.Name,
|
|
Type = account.Type,
|
|
Phone = account.Phone,
|
|
Email = account.Email,
|
|
Website = account.Website,
|
|
CreatedDate = account.CreatedDate,
|
|
CreatedBy = account.createdby
|
|
};
|
|
}
|
|
}
|
|
}
|