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; } }
public async Task <IActionResult> ClientModulePostTicket([FromRoute] Guid projectCode, [FromBody] TicketPostBase boundObject, ApiVersion version) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var projectDB = await _context.Projects.FirstOrDefaultAsync(p => p.Code == projectCode); if (projectDB == null) { return(Conflict(new { header = "Please contact the website admin.", subheader = "", text = "The project-id '" + projectCode + "' is not registered." })); } else if (projectDB.IsPaused) { return(Conflict(new { header = "Please be patient.", subheader = "", text = "This page is currently paused for feedback." })); } if (!String.IsNullOrEmpty(boundObject.IotaAddress)) { if (!IOTAHelper.IsAddress(boundObject.IotaAddress)) { return(BadRequest(new { header = "Input error", subheader = "", text = "The submitted IOTA address is not a valid address, please check it." })); } } if (!String.IsNullOrEmpty(boundObject.Email)) { if (!_regexUtil.IsValidEmail(boundObject.Email)) { return(BadRequest(new { header = "Input error", subheader = "", text = "The submitted email address is not a valid address, please check it." })); } } if (boundObject.Url.EndsWith("/")) { boundObject.Url.Remove(boundObject.Url.Length - 1); } Ticket ticket = new Ticket() { Name = boundObject.Name, Email = boundObject.Email, IotaAddress = boundObject.IotaAddress, NavigatorString = boundObject.NavigatorString, BrowserFontSize = boundObject.BrowserFontSize, ScreenHeight = boundObject.ScreenHeight, ScreenWidth = boundObject.ScreenWidth, Annotations = boundObject.Annotations, Sent = boundObject.Sent, IsPublic = boundObject.IsPublic, Url = boundObject.Url, ViewGuid = GUIDHelper.CreateCryptographicallySecureGuid(), // a test if same ViewGuid already exists would be good ProjectId = projectDB.Id }; _context.Tickets.Add(ticket); await _context.SaveChangesAsync(); //Task.Run(() => // not a good idea in ASP.net! //{ // Email.DefaultSender = new MailgunSender("", // Mailgun Domain // "" // Mailgun API Key // ); // var email = Email // .From("*****@*****.**") // .To(ticket.Email) // .Subject("Thanks for your feedback") // .Body("Thank you"); // email.Send(); //}); var domainDB = await _context.Domains.FirstOrDefaultAsync(d => d.Id == projectDB.DomainId); if (domainDB != null) { await _hubContext.Clients.All.SendAsync("TicketAdded", new { authIdentifier = domainDB.UserAuthIdentifier, domain = domainDB.Url, projectName = projectDB.Name, domainId = domainDB.Id, projectId = projectDB.Id, ticketId = ticket.Id, sent = ticket.Sent }); // would be better to take an extra (temporary) identifier, that the client-side stores } return(CreatedAtAction(nameof(ClientModuleGetByViewID), new { projectCode, viewGuid = ticket.ViewGuid, version = $"{version}" }, ticket)); }
public async Task <IActionResult> ClientModulePostAlternativesSelection([FromRoute] Guid projectCode, [FromBody] AlternativesSelectionPostBase boundObject, ApiVersion version) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var projectDB = await _context.Projects.FirstOrDefaultAsync(p => p.Code == projectCode); if (projectDB == null) { return(Conflict(new { header = "Please contact the website admin.", subheader = "", text = "The project-id '" + projectCode + "' is not registered." })); } else if (projectDB.IsPaused) { return(Conflict(new { header = "Please be patient.", subheader = "", text = "This page is currently paused for feedback." })); } if (!String.IsNullOrEmpty(boundObject.IotaAddress)) { if (!IOTAHelper.IsAddress(boundObject.IotaAddress)) { return(BadRequest(new { header = "Input error", subheader = "", text = "The submitted IOTA address is not a valid address, please check it." })); } } if (!String.IsNullOrEmpty(boundObject.Email)) { if (!_regexUtil.IsValidEmail(boundObject.Email)) { return(BadRequest(new { header = "Input error", subheader = "", text = "The submitted email address is not a valid address, please check it." })); } } if (boundObject.Url.EndsWith("/")) { boundObject.Url.Remove(boundObject.Url.Length - 1); } AlternativesSelection selection = new AlternativesSelection() { Name = boundObject.Name, Email = boundObject.Email, IotaAddress = boundObject.IotaAddress, Sent = boundObject.Sent, AreaInfoItems = boundObject.AreaInfoItems, Url = boundObject.Url, ViewGuid = GUIDHelper.CreateCryptographicallySecureGuid(), // a test if same ViewGuid already exists would be good ProjectId = projectDB.Id }; _context.AlternativesSelections.Add(selection); await _context.SaveChangesAsync(); var domainDB = await _context.Domains.FirstOrDefaultAsync(d => d.Id == projectDB.DomainId); if (domainDB != null) { await _hubContext.Clients.All.SendAsync("AlternativesSelectionAdded", new { authIdentifier = domainDB.UserAuthIdentifier, domain = domainDB.Url, projectName = projectDB.Name, domainId = domainDB.Id, projectId = projectDB.Id, alternativesSelectionId = selection.Id, sent = selection.Sent }); // would be better to take an extra (temporary) identifier, that the client-side stores } return(CreatedAtAction(nameof(ClientModuleGetByViewID), new { projectCode, viewGuid = selection.ViewGuid, version = $"{version}" }, selection)); }