public async Task <IActionResult> Put([FromBody] ProjectType projectType) { if (projectType is null) { throw new ArgumentNullException(nameof(projectType)); } var validation = new ProjectTypeValidator().Validate(projectType); if (!validation.IsValid) { return(ErrorResult .BadRequest(validation) .ActionResult()); } var existingProjectType = await projectTypesRepository .GetAsync(projectType.Id) .ConfigureAwait(false); if (existingProjectType is null) { return(ErrorResult .NotFound($"A ProjectType with the ID '{projectType.Id}' could not be found in this TeamCloud Instance") .ActionResult()); } var providers = await providersRepository .ListAsync() .ToListAsync() .ConfigureAwait(false); var validProviders = projectType.Providers .All(p => providers.Any(provider => provider.Id == p.Id)); if (!validProviders) { var validProviderIds = string.Join(", ", providers.Select(p => p.Id)); return(ErrorResult .BadRequest(new ValidationError { Field = "projectType", Message = $"All provider ids on a ProjectType must match the id of a registered Provider on the TeamCloud instance. Valid provider ids are: {validProviderIds}" }) .ActionResult()); } existingProjectType.PopulateFromExternalModel(projectType); var updateResult = await orchestrator .UpdateAsync(existingProjectType) .ConfigureAwait(false); var returnUpdateResult = updateResult.PopulateExternalModel(); return(DataResult <ProjectType> .Ok(returnUpdateResult) .ActionResult()); }
public async Task <IActionResult> Post([FromBody] ProjectType projectType) { if (projectType is null) { throw new ArgumentNullException(nameof(projectType)); } var validation = new ProjectTypeValidator().Validate(projectType); if (!validation.IsValid) { return(ErrorResult .BadRequest(validation) .ActionResult()); } var existingProjectType = await projectTypesRepository .GetAsync(projectType.Id) .ConfigureAwait(false); if (existingProjectType != null) { return(ErrorResult .Conflict($"A ProjectType with id '{projectType.Id}' already exists. Please try your request again with a unique id or call PUT to update the existing ProjectType.") .ActionResult()); } var providers = await providersRepository .ListAsync() .ToListAsync() .ConfigureAwait(false); var validProviders = projectType.Providers .All(p => providers.Any(provider => provider.Id == p.Id)); if (!validProviders) { var validProviderIds = string.Join(", ", providers.Select(p => p.Id)); return(ErrorResult .BadRequest(new ValidationError { Field = "projectType", Message = $"All provider ids on a ProjectType must match the id of a registered Provider on the TeamCloud instance. Valid provider ids are: {validProviderIds}" }) .ActionResult()); } var newProjectType = new Model.Internal.Data.ProjectType(); newProjectType.PopulateFromExternalModel(projectType); var addResult = await orchestrator .AddAsync(newProjectType) .ConfigureAwait(false); var baseUrl = HttpContext.GetApplicationBaseUrl(); var location = new Uri(baseUrl, $"api/projectTypes/{addResult.Id}").ToString(); var returnAddResult = addResult.PopulateExternalModel(); return(DataResult <ProjectType> .Created(returnAddResult, location) .ActionResult()); }