public async Task <IActionResult> PutProject([FromRoute] int domainID, [FromRoute] int id, [FromBody] ProjectPostAndPutBase boundObject) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { base.SetAuthIdentifierFromRequest(); var domainProjects = await QueryHelper.GetDomainProjectsAuthenticatedQuery(_context, _authIdentifier, domainID).ToListAsync(); var projectDB = domainProjects.FirstOrDefault(p => p.Id == id); if (projectDB == null) { return(NotFound(new { header = "The given project-id was not found in your projects", subheader = "", text = "Please check the id." })); } _context.Projects.Attach(projectDB); // to recoqnize changes // Update only relevant fields string emailToLower = boundObject.NotificationEmail.ToLower(); if (projectDB.NotificationEmail != emailToLower) { if (new RegexUtilities().IsValidEmail(emailToLower)) { projectDB.NotificationEmail = emailToLower; } } boundObject.Name = boundObject.Name.Trim(); if (domainProjects.Any(p => p.Name == boundObject.Name && p.Id != id)) { return(Conflict(new { header = "Conflict", subheader = "", text = "There is already a project with the name '" + boundObject.Name + "' in your projects." })); } projectDB.Name = boundObject.Name; projectDB.Description = boundObject.Description; projectDB.IsPaused = boundObject.IsPaused; await _context.SaveChangesAsync(); return(NoContent()); } catch (MissingAuthIdentifierException) { return(_statusCode); } catch { throw; } }
public async Task <IActionResult> PostProject([FromRoute] int domainID, [FromBody] ProjectPostAndPutBase boundObject, ApiVersion version) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { base.SetAuthIdentifierFromRequest(); if (!new RegexUtilities().IsValidEmail(boundObject.NotificationEmail)) { return(BadRequest(new { header = "Input error", subheader = "", text = "Please submit a valid email." })); } var domainDB = await QueryHelper.GetDomainByIDAuthenticatedAsync(_context, _authIdentifier, domainID); if (domainDB == null) { return(NotFound(new { header = "The given domain-id was not found in your domains", subheader = "", text = "Please check the id." })); } boundObject.Name = boundObject.Name.Trim(); await _context.Entry(domainDB).Collection(t => t.Projects).LoadAsync(); // load explicitly if (domainDB.Projects.Any(p => p.Name == boundObject.Name && p.DomainId == domainID)) { return(Conflict(new { header = "Conflict", subheader = "", text = "There is already a project with the name '" + boundObject.Name + "' in your projects." })); } // generate real Project Project project = new Project { Name = boundObject.Name, Description = boundObject.Description, Code = GUIDHelper.CreateCryptographicallySecureGuid(), // a test if same ViewGuid already exists would be good NotificationEmail = boundObject.NotificationEmail, IsPaused = boundObject.IsPaused, DomainId = domainID }; _context.Projects.Add(project); await _context.SaveChangesAsync(); return(CreatedAtAction(nameof(GetProject), new { domainID, id = project.Id, version = $"{version}" }, project)); } catch (MissingAuthIdentifierException) { return(_statusCode); } catch { throw; } }