Example #1
0
        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));
        }
Example #2
0
        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));
        }
Example #3
0
        public async Task <IActionResult> PutUser([FromBody] UserPutBase boundObject)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                base.SetAuthIdentifierFromRequest();

                var userDB = await _context.Users.SingleOrDefaultAsync(m => m.AuthIdentifier == _authIdentifier);

                if (userDB == null)
                {
                    return(NotFound(new
                    {
                        header = "The given user was not found",
                        subheader = "",
                        text = "Please register first."
                    }));
                }

                if (!new RegexUtilities().IsValidEmail(boundObject.Email))
                {
                    return(BadRequest(new
                    {
                        header = "Input error",
                        subheader = "",
                        text = "Please submit a valid email."
                    }));
                }

                if (!string.IsNullOrWhiteSpace(boundObject.IotaSeed) && !IOTAHelper.IsTrytes(boundObject.IotaSeed, 81))
                {
                    return(BadRequest(new
                    {
                        header = "Input error",
                        subheader = "",
                        text = "Please submit a valid seed of 81 characters from range A-Z and number 9."
                    }));
                }

                _context.Users.Attach(userDB); // to recoqnize changes

                userDB.Email    = boundObject.Email;
                userDB.IotaSeed = boundObject.IotaSeed;
                userDB.IotaNode = boundObject.IotaNode;

                await _context.SaveChangesAsync();

                return(NoContent());
            }
            catch (MissingAuthIdentifierException)
            {
                return(_statusCode);
            }
            catch
            {
                throw;
            }
        }