public async Task <GetCubicleDto> DeleteOneByIdAsync(int id) { var item = await _cubicleRepository.FindOneByIdAsync(id); item = await _cubicleRepository.DeleteOneAsync(item); return(GetCubicleDto.FromCubicle(item)); }
public async Task <GetCubicleDto> FindOneByCodeAsync(string code) { var item = await _cubicleRepository.FindOneByCodeAsync(code); return(GetCubicleDto.FromCubicle(item)); }
public async Task <GetCubicleDto> CreateOneAsync(CreateCubicleDto createCubicle) { // Validando que el cubiculo no exista en la base de datos var found = await _cubicleRepository.FindOneByCodeAsync(createCubicle.Code); if (found != null) { throw new BadRequestException($"Cubicle with code {createCubicle.Code} already exists"); } var resources = new List <Resource>(); foreach (var resourceId in createCubicle.ResourcesIds) { // Validando la existencia de cada recurso var resource = await _resourceRepository.FindOneByIdAsync(resourceId); if (resource == null) { throw new BadRequestException($"Resource with id {resourceId} was not found"); } // Validando que el recurso no le pertenezca a otro cubículo if (resource.CubicleId.HasValue) { throw new BadRequestException($"The resource with id {resource.Id} already has a Cubicle"); } resources.Add(resource); } // Se valida la existencia del campus var campus = await _campusRepository.GetOneByIdAsync(createCubicle.CampusId); if (campus == null) { throw new BadRequestException($"CampusId with id {createCubicle.CampusId} was not found"); } var cubicle = new Cubicle { Code = createCubicle.Code, Description = createCubicle.Description, CampusId = campus.Id, Pavilion = createCubicle.PavilionId, IsActive = createCubicle.IsActive, CreatedAt = new DateTime(), UpdatedAt = new DateTime(), TotalSeats = createCubicle.TotalSeats, }; await _cubicleRepository.CreateOneAsync(cubicle); foreach (var resource in resources) { resource.CubicleId = cubicle.Id; await _resourceRepository.UpdateOneByIdAsync(resource.Id, resource); } await _context.SaveChangesAsync(); return(GetCubicleDto.FromCubicle(cubicle)); }