Example #1
0
        public ActionResult AddOrUpdateProjects(int id, [FromBody] JsonElement jsonElement)
        {
            // Converts json element to string
            string json = System.Text.Json.JsonSerializer.Serialize(jsonElement);

            // Converts json string to JSON object
            var obj = JsonConvert.DeserializeObject <JObject>(json);

            try
            {
                // Converts nested "Projects"-JSON object to an array and save count of an array to variable
                var projectsArray      = obj["Projects"].ToArray();
                int projectsArrayCount = projectsArray.Count();

                for (int a = 0; a < projectsArrayCount; a++)
                {
                    int projectId = int.Parse(projectsArray[a]["ProjectId"].ToString());

                    // If the project is new (projectId == 0), creation is made to database. Otherwise an update
                    if (projectId == 0)
                    {
                        Projects project = new Projects()
                        {
                            SkillId     = id,
                            Name        = projectsArray[a]["Name"].ToString(),
                            Link        = projectsArray[a]["Link"].ToString(),
                            Description = projectsArray[a]["Description"].ToString()
                        };

                        _context.Projects.Add(project);
                        _context.SaveChanges();
                    }
                    else
                    {
                        // Updates the project
                        Projects project = new Projects
                        {
                            Name        = projectsArray[a]["Name"].ToString(),
                            Link        = projectsArray[a]["Link"].ToString(),
                            Description = projectsArray[a]["Description"].ToString()
                        };

                        if (!UpdateProject(projectId, project))
                        {
                            return(BadRequest("Problem detected while updating project for skill " + id + "."));
                        }
                    }
                }

                return(Ok("New project(s) has added/updated to a skill!"));
            }
            catch (Exception ex)
            {
                return(BadRequest("Problem detected while adding a new project. Error message: " + ex.Message));
            }
            finally
            {
                _context.Dispose();
            }
        }
Example #2
0
        public ActionResult AddOrUpdateLinks(int id, [FromBody] JsonElement jsonElement)
        {
            // Converts json element to string
            string json = System.Text.Json.JsonSerializer.Serialize(jsonElement);

            // Converts json string to JSON object
            var obj = JsonConvert.DeserializeObject <JObject>(json);

            try
            {
                // Converts nested "Services" JSON object to an array and save count of an array to variable
                var servicesArray      = obj["Services"].ToArray();
                int servicesArrayCount = servicesArray.Count();

                // Adds as many links as the count of an array indicates
                for (int a = 0; a < servicesArrayCount; a++)
                {
                    int linkId = int.Parse(servicesArray[a]["LinkId"].ToString());

                    // If the link is new (linkId == 0), a create operation is made to database. Otherwise an update
                    if (linkId == 0)
                    {
                        SocialMediaLinks link = new SocialMediaLinks
                        {
                            UserId    = id,
                            ServiceId = int.Parse(servicesArray[a]["ServiceId"].ToString()),
                            Link      = servicesArray[a]["Link"].ToString()
                        };

                        _context.SocialMediaLinks.Add(link);
                        _context.SaveChanges();
                    }
                    else
                    {
                        // Updates the link
                        SocialMediaLinks link = new SocialMediaLinks
                        {
                            UserId    = id,
                            ServiceId = int.Parse(servicesArray[a]["ServiceId"].ToString()),
                            Link      = servicesArray[a]["Link"].ToString()
                        };

                        if (!UpdateLink(linkId, link))
                        {
                            return(BadRequest("Problem detected while updating social media link. Link ID: " + linkId));
                        }
                    }
                }

                return(Ok("New links has added to portfolio!"));
            }
            catch (Exception ex)
            {
                return(BadRequest("Problem detected while adding a new link. Error message: " + ex.Message));
            }
            finally
            {
                _context.Dispose();
            }
        }
        public ActionResult AddContent(int id, [FromBody] PortfolioContent newContent)
        {
            try
            {
                // Adding content to database (except emails)
                PortfolioContent newPortfolio = new PortfolioContent
                {
                    UserId         = id,
                    Firstname      = newContent.Firstname,
                    Lastname       = newContent.Lastname,
                    Birthdate      = newContent.Birthdate,
                    City           = newContent.City,
                    Country        = newContent.Country,
                    Phonenumber    = newContent.Phonenumber,
                    Punchline      = newContent.Punchline,
                    BasicKnowledge = newContent.BasicKnowledge,
                    Education      = newContent.Education,
                    WorkHistory    = newContent.WorkHistory,
                    LanguageSkills = newContent.LanguageSkills
                };

                _context.PortfolioContent.Add(newPortfolio);
                _context.SaveChanges();

                return(Ok("New content has saved!"));
            }
            catch (Exception ex)
            {
                return(BadRequest("Problem detected while adding portfolio content for user " + newContent.Firstname + " " + newContent.Lastname + ". Error message: " + ex.Message));
            }
            finally
            {
                _context.Dispose();
            }
        }
Example #4
0
        private string AddSasToUser(WebPortfolioContext _context, string username)
        {
            // Search the user which has added recently
            Users addedUser = (from u in _context.Users
                               where u.Username == username
                               select u).FirstOrDefault();

            // Storage account
            var storageAccount = CloudStorageAccount.Parse(connectionString);

            // Generate a SAS token for the user's container/object to Webportfolio's Storage Account
            SharedAccessAccountPolicy policy = new SharedAccessAccountPolicy()
            {
                Permissions = SharedAccessAccountPermissions.Write |
                              SharedAccessAccountPermissions.Create |
                              SharedAccessAccountPermissions.Read |
                              SharedAccessAccountPermissions.Delete |
                              SharedAccessAccountPermissions.Add |
                              SharedAccessAccountPermissions.List,
                Services               = SharedAccessAccountServices.Blob,
                ResourceTypes          = SharedAccessAccountResourceTypes.Object | SharedAccessAccountResourceTypes.Container,
                SharedAccessStartTime  = DateTime.UtcNow.AddHours(-1),
                SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1),
                Protocols              = SharedAccessProtocol.HttpsOnly,
            };
            string sasToken = storageAccount.GetSharedAccessSignature(policy);

            // Save the SAS token to database
            addedUser.SasToken = sasToken;
            _context.SaveChanges();

            return(sasToken);
        }
Example #5
0
        public ActionResult AddNewUser([FromBody] Users newUser)
        {
            try
            {
                _context.Users.Add(newUser);

                // Add a SAS token and the portfolio content with default values to a new user
                if (_context.SaveChanges() > 0)
                {
                    AddSasToUser(_context, newUser.Username);

                    PortfolioContentController controller = new PortfolioContentController(_context);

                    controller.AddDefaultContent(newUser.Username);
                    controller.AddDefaultEmails(newUser.Username);
                }

                return(Ok("New user has created and the portfolio content with default values has added!"));
            }
            catch (Exception ex)
            {
                return(BadRequest("Problem detected while adding a new user. Error message: " + ex.Message));
            }
            finally
            {
                _context.Dispose();
            }
        }
Example #6
0
        public ActionResult AddOrUpdateSkills(int id, [FromBody] JsonElement jsonElement)
        {
            // Converts json element to string
            string json = System.Text.Json.JsonSerializer.Serialize(jsonElement);

            // Converts json string to JSON object
            var obj = JsonConvert.DeserializeObject <JObject>(json);

            try
            {
                // Converts nested "Skills"-JSON object to an array and save count of an array to variable
                var skillsArray     = obj["Skills"].ToArray();
                int skillArrayCount = skillsArray.Count();

                // Adds as many skills as the count of an array indicates
                for (int i = 0; i < skillArrayCount; i++)
                {
                    int skillId = int.Parse(skillsArray[i]["SkillId"].ToString());
                    // If skill is new (skillId == 0), creation is made to database. Otherwise an update
                    if (skillId == 0)
                    {
                        // Adds new skill to database
                        Skills skill = new Skills
                        {
                            UserId     = id,
                            Skill      = skillsArray[i]["Skill"].ToString(),
                            SkillLevel = int.Parse(skillsArray[i]["SkillLevel"].ToString())
                        };

                        _context.Skills.Add(skill);
                        _context.SaveChanges();
                    }
                    else
                    {
                        // Updates the skill
                        Skills skill = new Skills
                        {
                            Skill      = skillsArray[i]["Skill"].ToString(),
                            SkillLevel = int.Parse(skillsArray[i]["SkillLevel"].ToString())
                        };

                        if (!UpdateSkill(skillId, skill))
                        {
                            return(BadRequest("Problem detected while updating the skill for user " + id + "."));
                        }
                    }
                }

                return(Ok("Skills has saved!"));
            }
            catch (Exception ex)
            {
                return(BadRequest("Problem detected while adding skills for user " + id + ". Error message: " + ex.InnerException.Message));
            }
            finally
            {
                _context.Dispose();
            }
        }
Example #7
0
        public ActionResult AddImages(int id, [FromBody] JsonElement jsonElement)
        {
            // Converts json element to string
            string json = System.Text.Json.JsonSerializer.Serialize(jsonElement);

            // Converts json string to dataset
            DataSet dataSet = JsonConvert.DeserializeObject <DataSet>(json);

            try
            {
                if (dataSet != null)
                {
                    // Get through every table from dataset
                    // Tables includes type ID and new URL for image
                    foreach (DataTable dataTable in dataSet.Tables)
                    {
                        DataRow row = dataTable.Rows[0];

                        int    typeId = int.Parse(row["TypeID"].ToString());
                        string url    = row["Url"].ToString();

                        ImageUrls oldImage = (from iu in _context.ImageUrls
                                              where iu.UserId == id && iu.TypeId == typeId
                                              select iu).FirstOrDefault();

                        if (oldImage != null)
                        {
                            // If image url has changed, it will be updated to database
                            if (url != oldImage.Url)
                            {
                                oldImage.Url = url;
                                _context.SaveChanges();
                            }
                        }
                        else
                        {
                            // Adds image urls to database
                            ImageUrls newImage = new ImageUrls
                            {
                                UserId = id,
                                TypeId = typeId,
                                Url    = url
                            };

                            _context.ImageUrls.Add(newImage);
                            _context.SaveChanges();
                        }
                    }
                }

                return(Ok("The Image has added/updated succesfully!"));
            }
            catch (Exception ex)
            {
                return(BadRequest("Problem detected while adding image for user " + id + ". Error message: " + ex.Message));
            }
            finally
            {
                _context.Dispose();
            }
        }
        public ActionResult AddNewMessage(int id, [FromBody] NewMessage newMessage)
        {
            try
            {
                // Check if a visitor already exists in the database
                var existingVisitor = (from v in _context.Visitors
                                       where v.Firstname == newMessage.VisitorFirstname &&
                                       v.Lastname == newMessage.VisitorLastname &&
                                       v.Company == newMessage.VisitorCompany
                                       select v).FirstOrDefault();

                int visitorId = 0;

                if (existingVisitor == null)
                {
                    // If a visitor is new, it will be added to database
                    // Placed visitor info to an object and adding it to database
                    Visitors visitor = new Visitors
                    {
                        Firstname = newMessage.VisitorFirstname,
                        Lastname  = newMessage.VisitorLastname,
                        Company   = newMessage.VisitorCompany
                    };

                    _context.Visitors.Add(visitor);
                    _context.SaveChanges();

                    // Searching for last added visitor ID and adding other message and other information to database
                    visitorId = (from v in _context.Visitors
                                 orderby v.VisitorId ascending
                                 select v.VisitorId).LastOrDefault();
                }
                else
                {
                    // If a visitor already exists in the database
                    visitorId = existingVisitor.VisitorId;
                }

                // New message to database
                QuestbookMessages messageAndOthers = new QuestbookMessages
                {
                    UserId              = id,
                    VisitorId           = visitorId,
                    Message             = newMessage.Message,
                    VisitationTimestamp = newMessage.VisitationTimestamp
                };

                _context.QuestbookMessages.Add(messageAndOthers);
                _context.SaveChanges();

                return(Ok("New message has saved!"));
            }
            catch (Exception ex)
            {
                return(BadRequest("Problem detected while adding a new message. Error message: " + ex.Message));
            }
            finally
            {
                _context.Dispose();
            }
        }