avoid duplicate person names
This commit is contained in:
parent
60c74da842
commit
8e2342fd74
|
|
@ -52,6 +52,12 @@ public class PersonService : IPersonService
|
|||
/// <inheritdoc />
|
||||
public async Task<PersonDto> CreateAsync(CreatePersonDto dto, CancellationToken ct = default)
|
||||
{
|
||||
var existingPerson = await _personRepository.GetByNameAsync(_clubContext.ClubId, dto.Name, ct);
|
||||
if (existingPerson != null)
|
||||
{
|
||||
throw new InvalidOperationException($"Person with name '{dto.Name}' already exists");
|
||||
}
|
||||
|
||||
var entity = new Person
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
|
|
@ -75,6 +81,12 @@ public class PersonService : IPersonService
|
|||
if (existing.ClubId != _clubContext.ClubId)
|
||||
throw new InvalidOperationException("Person does not belong to current club.");
|
||||
|
||||
var existingPerson = await _personRepository.GetByNameAsync(_clubContext.ClubId, dto.Name, ct);
|
||||
if (existingPerson != null)
|
||||
{
|
||||
throw new InvalidOperationException($"Person with name '{dto.Name}' already exists");
|
||||
}
|
||||
|
||||
existing.Name = dto.Name;
|
||||
existing.PersonStatus = dto.PersonStatus;
|
||||
existing.ModifiedAt = DateTime.UtcNow;
|
||||
|
|
|
|||
|
|
@ -46,4 +46,13 @@ public interface IPersonRepository
|
|||
/// <param name="ct">Cancellation token.</param>
|
||||
/// <returns>True if deleted successfully; otherwise, false.</returns>
|
||||
Task<bool> DeleteAsync(Guid id, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Read a person by its name within a specific club context.
|
||||
/// </summary>
|
||||
/// <param name="clubContextClubId"></param>
|
||||
/// <param name="dtoName"></param>
|
||||
/// <param name="ct"></param>
|
||||
/// <returns>The person with the name or null</returns>
|
||||
Task<Person?> GetByNameAsync(Guid clubContextClubId, string dtoName, CancellationToken ct);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,4 +63,13 @@ public class PersonRepository(IDbContextFactory<AppDbContext> contextFactory) :
|
|||
await context.SaveChangesAsync(ct);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<Person?> GetByNameAsync(Guid clubContextClubId, string dtoName, CancellationToken ct)
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(ct);
|
||||
var entity = await context.Persons
|
||||
.FirstOrDefaultAsync(p => p.ClubId == clubContextClubId && p.Name == dtoName && !p.IsDeleted, ct);
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue