public void TestDeserialize()
        {
            Assert.AreEqual(1, UserToTextEntry.Manipulator.InsertDataInto(TestConnection, TableName, Id1));
            UserToTextEntry toTest = UserToTextEntry.Manipulator.RetrieveDataWhere(TestConnection, TableName, "Text=\"" + Id1.Text + "\"")[0];

            Assert.AreEqual(Id1, toTest);
            Assert.AreEqual(1, UserToTextEntry.Manipulator.RemoveDataWhere(TestConnection, TableName, "Text=\"" + Id1.Text + "\""));
        }
Esempio n. 2
0
        private JsonDictionaryStringConstructor ConvertForumPostToJson(UserToTextEntry forumPost, MySqlDataManipulator connection)
        {
            JsonDictionaryStringConstructor retConstructor = new JsonDictionaryStringConstructor();
            var user = connection.GetUserById(forumPost.UserId);

            if (user == null)
            {
                retConstructor.SetMapping("DisplayName", "Unknown User");
            }
            else
            {
                List <UserSettingsEntry> userSettings = JsonDataObjectUtil <List <UserSettingsEntry> > .ParseObject(user.Settings);

                retConstructor.SetMapping("DisplayName", userSettings.Where(entry => entry.Key.Equals(UserSettingsEntryKeys.DisplayName)).First().Value);
            }
            retConstructor.SetMapping("PostText", forumPost.Text);
            retConstructor.SetMapping("ForumPostId", forumPost.Id);
            return(retConstructor);
        }
Esempio n. 3
0
        /// <summary>
        /// DELETE request format located in the Web Api Enumeration v2
        /// under the tab Company/Forum, starting row 26
        /// </summary>
        /// <param name="ctx">HttpListenerContext to respond to</param>
        private void HandleDeleteRequest(HttpListenerContext ctx)
        {
            try
            {
                #region Input Validation
                if (!ctx.Request.HasEntityBody)
                {
                    WriteBodyResponse(ctx, 400, "Bad Request", "No Body");
                    return;
                }

                CompanyForumApiDeleteRequest entry = JsonDataObjectUtil <CompanyForumApiDeleteRequest> .ParseObject(ctx);

                if (!ValidateDeleteRequest(entry))
                {
                    WriteBodyResponse(ctx, 400, "Bad Request", "Incorrect Format");
                    return;
                }
                #endregion

                //Otherwise we have a valid entry, validate user
                MySqlDataManipulator connection = new MySqlDataManipulator();
                using (connection)
                {
                    bool res = connection.Connect(MySqlDataManipulator.GlobalConfiguration.GetConnectionString());
                    if (!res)
                    {
                        WriteBodyResponse(ctx, 500, "Unexpected ServerError", "Connection to database failed");
                        return;
                    }
                    #region User Validation
                    OverallUser mappedUser = connection.GetUserById(entry.UserId);
                    if (mappedUser == null)
                    {
                        WriteBodyResponse(ctx, 404, "Not Found", "User was not found on the server");
                        return;
                    }
                    if (!UserVerificationUtil.LoginTokenValid(mappedUser, entry.LoginToken))
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "Login token was incorrect.");
                        return;
                    }
                    if (!UserVerificationUtil.AuthTokenValid(mappedUser, entry.AuthToken))
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "Auth token was expired or incorrect");
                        return;
                    }
                    #endregion

                    #region Delete Forum
                    UserToTextEntry forumPost = connection.GetForumPostById(mappedUser.Company, entry.JobEntryId, entry.ForumPostId);
                    if (forumPost == null)
                    {
                        WriteBodylessResponse(ctx, 404, "Not Found");
                        return;
                    }
                    if (forumPost.UserId != entry.UserId)
                    {
                        WriteBodyResponse(ctx, 401, "Unauthorized", "Cannot delete the post of a different user");
                        return;
                    }
                    if (!connection.RemoveForumPost(mappedUser.Company, entry.JobEntryId, forumPost))
                    {
                        WriteBodyResponse(ctx, 500, "Internal Server Error", "Error occured while removing forum post: " + connection.LastException.Message);
                        return;
                    }
                    WriteBodylessResponse(ctx, 200, "OK");
                    #endregion
                }
            }
            catch (HttpListenerException)
            {
                //HttpListeners dispose themselves when an exception occurs, so we can do no more.
            }
            catch (Exception e)
            {
                WriteBodyResponse(ctx, 500, "Internal Server Error", e.Message);
            }
        }
 public void Init()
 {
     Id1 = new UserToTextEntry(1, "*****@*****.**");
     Id2 = new UserToTextEntry(2, "*****@*****.**");
     Id3 = new UserToTextEntry(1, "*****@*****.**");
 }