Ejemplo n.º 1
0
        public List <UserPost> Get(UserPostRequest request)
        {
            var           userPosts    = new List <UserPost>();
            UserPost      post         = new UserPost();
            var           dbConnection = new DatabaseConnector();
            var           paramsList   = new List <SqlParameter>();
            SqlDataReader dataReader;
            string        command;

            try
            {
                if (request.Id == 0)
                {
                    command    = "Select * from Wall";
                    dataReader = dbConnection.executeCommand(command, paramsList);

                    while (dataReader.Read())
                    {
                        userPosts.Add(new UserPost
                        {
                            id         = dataReader.GetInt32(0),
                            authorId   = dataReader.GetInt32(1),
                            title      = dataReader.GetString(2),
                            reblog     = dataReader.GetInt32(3),
                            text       = dataReader.GetString(4),
                            attachment = dataReader.GetString(6),
                            date       = dataReader.GetDateTime(7)
                        });
                    }
                    dataReader.Close();

                    return(userPosts);
                }
                else
                {
                    paramsList.Clear();

                    SqlParameter loginParam = new SqlParameter("@Id", SqlDbType.Int);
                    loginParam.Value = request.Id;
                    paramsList.Add(loginParam);
                    command    = "Select * from Wall where id=@Id";
                    dataReader = dbConnection.executeCommand(command, paramsList);

                    if (dataReader.HasRows)
                    {
                        dataReader.Read();

                        userPosts.Add(new UserPost
                        {
                            id         = dataReader.GetInt32(0),
                            authorId   = dataReader.GetInt32(1),
                            title      = dataReader.GetString(2),
                            reblog     = dataReader.GetInt32(3),
                            text       = dataReader.GetString(4),
                            attachment = dataReader.GetString(6),
                            date       = dataReader.GetDateTime(7)
                        });

                        dataReader.Close();

                        return(userPosts);
                    }
                    else
                    {
                        dataReader.Close();
                        return(null);
                    }
                }
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
        public int Post(UserPostsRequest request)
        {
            UserPost      post         = new UserPost();
            List <Int32>  posts        = new List <Int32>();
            var           dbConnection = new DatabaseConnector();
            var           paramsList   = new List <SqlParameter>();
            SqlDataReader dataReader;
            string        command;

            try
            {
                SqlParameter loginParam = new SqlParameter("@Login", SqlDbType.VarChar, request.Login.Length);
                loginParam.Value = request.Login;
                paramsList.Add(loginParam);
                command    = "select id from Users where Login=@Login";
                dataReader = dbConnection.executeCommand(command, paramsList);

                dataReader.Read();

                post.authorId   = dataReader.GetInt32(0);
                post.title      = request.Title;
                post.text       = request.Content;
                post.tags       = 0;
                post.reblog     = 0;
                post.attachment = "empty";

                dataReader.Close();

                command = "INSERT into Wall (Autor, Tytul, reblog, Tresc, Tagi, attachment, DataPosta) values (@AuthorId, @Title, @Reblog, @Text, @Tags, @Attach, GETDATE())";

                paramsList.Clear();
                SqlParameter tempParam = new SqlParameter("@AuthorId", SqlDbType.Int);
                tempParam.Value = post.authorId;
                paramsList.Add(tempParam);
                tempParam       = new SqlParameter("@Title", SqlDbType.VarChar, post.title.Length);
                tempParam.Value = post.title;
                paramsList.Add(tempParam);
                tempParam       = new SqlParameter("@Reblog", SqlDbType.Int, post.reblog);
                tempParam.Value = post.reblog;
                paramsList.Add(tempParam);
                tempParam       = new SqlParameter("@Text", SqlDbType.VarChar, post.text.Length);
                tempParam.Value = post.text;
                paramsList.Add(tempParam);
                tempParam       = new SqlParameter("@Tags", SqlDbType.Binary);
                tempParam.Value = BitConverter.GetBytes(post.tags);
                paramsList.Add(tempParam);
                tempParam       = new SqlParameter("@Attach", SqlDbType.VarChar, post.attachment.Length);
                tempParam.Value = post.attachment;
                paramsList.Add(tempParam);

                dataReader = dbConnection.executeCommand(command, paramsList);

                if (dataReader.RecordsAffected > 0)
                {
                    return((int)HttpStatusCode.OK);
                }
                else
                {
                    return((int)HttpStatusCode.InternalServerError);
                }
            }
            catch (Exception ex)
            {
                return(0);
            }
        }
Ejemplo n.º 3
0
        public List <UserPost> Patch(UserPostRequest request)
        {
            var           userPosts    = new List <UserPost>();
            var           dbConnection = new DatabaseConnector();
            var           paramsList   = new List <SqlParameter>();
            SqlDataReader dataReader;
            string        command;

            try
            {
                SqlParameter loginParam = new SqlParameter("@Id", SqlDbType.Int);
                loginParam.Value = request.Id;
                paramsList.Add(loginParam);
                command    = "Select * from Wall where id=@Id";
                dataReader = dbConnection.executeCommand(command, paramsList);

                if (dataReader.HasRows)
                {
                    dataReader.Read();

                    userPosts.Add(new UserPost
                    {
                        id         = dataReader.GetInt32(0),
                        authorId   = dataReader.GetInt32(1),
                        title      = dataReader.GetString(2),
                        reblog     = dataReader.GetInt32(3),
                        text       = dataReader.GetString(4),
                        attachment = dataReader.GetString(6),
                        date       = dataReader.GetDateTime(7)
                    });

                    dataReader.Close();

                    //update post

                    userPosts.ElementAt(0).title = request.Title;
                    userPosts.ElementAt(0).text  = request.Content;

                    command = "UPDATE Wall set Tytul=@Title, Tresc=@Content where id=@IId";
                    paramsList.Clear();

                    SqlParameter tempParam = new SqlParameter("@Title", SqlDbType.VarChar, request.Title.Length);
                    tempParam.Value = request.Title;
                    paramsList.Add(tempParam);
                    tempParam       = new SqlParameter("@Content", SqlDbType.VarChar, request.Content.Length);
                    tempParam.Value = request.Content;
                    paramsList.Add(tempParam);
                    tempParam       = new SqlParameter("@IId", SqlDbType.Int);
                    tempParam.Value = request.Id;
                    paramsList.Add(tempParam);

                    dataReader = dbConnection.executeCommand(command, paramsList);

                    if (dataReader.RecordsAffected > 0)
                    {
                        dataReader.Close();
                        return(userPosts);
                    }
                    else
                    {
                        dataReader.Close();
                        return(null);
                    }
                }
                else
                {
                    dataReader.Close();
                    return(null);
                }
            }
            catch (Exception ex)
            {
                return(null);
            }
        }