Initial code commit.

This commit is contained in:
2026-02-03 10:44:31 +08:00
parent 8927c5ae0e
commit d69fe2cc1f
99 changed files with 10839 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
using AutoMapper;
using MediatR;
using Sufi.Demo.PeopleDirectory.Application.Contracts.Repositories;
using Sufi.Demo.PeopleDirectory.Application.Contracts.Services;
using Sufi.Demo.PeopleDirectory.Domain.Entities.Misc;
using Sufi.Demo.PeopleDirectory.Shared.Wrapper;
namespace Sufi.Demo.PeopleDirectory.Application.Features.Contacts.Queries.GetAll
{
public class GetAllContactsQuery : IRequest<IResult<List<GetAllContactsResponse>>>
{
}
public class GetAllContactsQueryHandler(
IUnitOfWork<int> unitOfWork,
IMapper mapper,
IAppCache appCache
) : IRequestHandler<GetAllContactsQuery, IResult<List<GetAllContactsResponse>>>
{
public async Task<IResult<List<GetAllContactsResponse>>> Handle(GetAllContactsQuery request, CancellationToken cancellationToken)
{
Task<List<Contact>> allContactsFunc() => unitOfWork.Repository<Contact>().GetAllAsync();
var allContacts = await appCache.GetOrAddAsync(
"contact_all",
async token => await allContactsFunc(),
absoluteExpireTime: TimeSpan.FromMinutes(2),
tags: ["contacts"]
);
var mappedContacts = mapper.Map<List<GetAllContactsResponse>>(allContacts);
return await Result<List<GetAllContactsResponse>>.SuccessAsync(mappedContacts);
}
}
}

View File

@@ -0,0 +1,12 @@
namespace Sufi.Demo.PeopleDirectory.Application.Features.Contacts.Queries.GetAll
{
public record GetAllContactsResponse
{
public int Id { get; set; }
public string UserName { get; set; } = null!;
public string Phone { get; set; } = null!;
public string Email { get; set; } = null!;
public string SkillSets { get; set; } = null!;
public string Hobby { get; set; } = null!;
}
}

View File

@@ -0,0 +1,47 @@
using AutoMapper;
using FluentValidation;
using MediatR;
using Sufi.Demo.PeopleDirectory.Application.Contracts.Repositories;
using Sufi.Demo.PeopleDirectory.Application.Contracts.Services;
using Sufi.Demo.PeopleDirectory.Domain.Entities.Misc;
using Sufi.Demo.PeopleDirectory.Shared.Wrapper;
namespace Sufi.Demo.PeopleDirectory.Application.Features.Contacts.Queries.GetById
{
public class GetContactByIdQuery : IRequest<IResult<GetContactByIdResponse>>
{
public int Id { get; set; }
}
public sealed class GetContactByIdQueryValidator : AbstractValidator<GetContactByIdQuery>
{
public GetContactByIdQueryValidator()
{
RuleFor(v => v.Id)
.GreaterThan(0)
.WithMessage("A valid Id is required.");
}
}
public class GetContactByIdQueryHandler(
IUnitOfWork<int> unitOfWork,
IMapper mapper,
IAppCache appCache
) : IRequestHandler<GetContactByIdQuery, IResult<GetContactByIdResponse>>
{
public async Task<IResult<GetContactByIdResponse>> Handle(GetContactByIdQuery request, CancellationToken cancellationToken)
{
Task<Contact?> getContactByIdFunc() => unitOfWork.Repository<Contact>().GetByIdAsync(request.Id);
var contact = await appCache.GetOrAddAsync(
$"contact_{request.Id}",
async token => await getContactByIdFunc(),
absoluteExpireTime: TimeSpan.FromMinutes(2),
tags: ["contacts"]
);
var mappedContact = mapper.Map<GetContactByIdResponse>(contact);
return await Result<GetContactByIdResponse>.SuccessAsync(mappedContact);
}
}
}

View File

@@ -0,0 +1,12 @@
namespace Sufi.Demo.PeopleDirectory.Application.Features.Contacts.Queries.GetById
{
public record GetContactByIdResponse
{
public int Id { get; set; }
public string UserName { get; set; } = null!;
public string Phone { get; set; } = null!;
public string Email { get; set; } = null!;
public string SkillSets { get; set; } = null!;
public string Hobby { get; set; } = null!;
}
}