public async Task <ServiceTypeDto> CreateAsync(ServiceTypeCreateDto item) { if (await _serviceTypeRepository.AnyServiceAsync(x => x.Name == item.Name)) { throw new FieldLogicException($"Service type with name {item.Name} already exists!", nameof(item.Name)); } var service = Mapper.Map <ServiceType>(item); _serviceTypeRepository.Create(service); await SaveProvider.SaveAsync(); return(Mapper.Map <ServiceTypeDto>(service)); }
public async Task <ActionResult <ServiceType> > CreateServiceType([FromBody] ServiceType serviceType) { try { await _repository.Create(serviceType); return(CreatedAtRoute("ServiceType", new { id = serviceType.ServiceTypeId }, serviceType)); } catch (Exception exc) { _logger.LogError($"Error: {exc}"); // transaction.Rollback(); return(NotFound()); } }
public void EnsureSeedData( IServiceStatusRepository serviceStatusRepository, IServiceTypeRepository serviceTypeRepository, IServiceProviderRepository serviceProviderRepository, IServiceSettings serviceSettings ) { PropertyInfo[] properties; properties = serviceSettings.ServiceTypeNames.GetType().GetProperties(); foreach (var property in properties) { var value = serviceSettings.ServiceTypeNames.GetType() .GetProperty(property.Name) .GetValue(serviceSettings.ServiceTypeNames); string typedValue = (string)value; if (serviceTypeRepository.GetSingle(x => x.Name.Equals(typedValue)) == null) { serviceTypeRepository.Create(new ServiceType { Name = typedValue }); } } properties = serviceSettings.ServiceStatusesSettings.GetType().GetProperties(); foreach (var property in properties) { var value = serviceSettings.ServiceStatusesSettings.GetType() .GetProperty(property.Name) .GetValue(serviceSettings.ServiceStatusesSettings); var typedValue = (ServiceStatusSettings)value; var serviceStatus = serviceStatusRepository.GetSingle(x => x.Name.Equals(typedValue.Name)); if (serviceStatus == null) { serviceStatusRepository.Create(new ServiceStatus { Name = typedValue.Name, Description = typedValue.Description }); } else { serviceStatus.Name = typedValue.Name; serviceStatus.Description = typedValue.Description; serviceStatusRepository.Update(serviceStatus); } } properties = serviceSettings.ServiceProviderTypeSettings.GetType().GetProperties(); foreach (var property in properties) { var value = serviceSettings.ServiceProviderTypeSettings.GetType() .GetProperty(property.Name) .GetValue(serviceSettings.ServiceProviderTypeSettings); var typedValue = (ServiceProviderSettings)value; var serviceProvider = serviceProviderRepository.GetSingle(x => x.Name.Equals(typedValue.Name)); if (serviceProvider == null) { var serviceType = serviceTypeRepository.GetSingle(x => x.Name.Equals(property.Name)); if (serviceType == null) { continue; } serviceProviderRepository.Create(new ServiceProvider { Name = typedValue.Name, Description = typedValue.Description, WebSite = typedValue.WebSite, ServiceTypeId = serviceType.Id }); } else { serviceProvider.Name = typedValue.Name; serviceProvider.Description = typedValue.Description; serviceProvider.WebSite = typedValue.WebSite; serviceProviderRepository.Update(serviceProvider); } } }