Example #1
0
        public bool AddPost(PostReq req)
        {
            var cnn = (SqlConnection)Context.Database.GetDbConnection();

            if (cnn.State == ConnectionState.Closed)
            {
                cnn.Open();
            }
            try
            {
                SqlDataAdapter da  = new SqlDataAdapter();
                DataSet        ds  = new DataSet();
                var            cmd = cnn.CreateCommand();
                cmd.CommandText = "AddPost";
                cmd.Parameters.AddWithValue("@AccountId", req.AccountId);
                cmd.Parameters.AddWithValue("@PostName", req.PostName);
                cmd.Parameters.AddWithValue("@Content", req.Content);
                cmd.Parameters.AddWithValue("@Address", req.Address);
                cmd.Parameters.AddWithValue("@PictureURL", req.PictureURL);
                cmd.CommandType  = CommandType.StoredProcedure;
                da.SelectCommand = cmd; //thuc hien
                da.Fill(ds);
            }
            catch (Exception ex)
            {
                return(false);
            }
            return(true);
        }
        public SingleRsp CreatePost(PostReq post)
        {
            var res     = new SingleRsp();
            var postNew = new Posts()
            {
                AccountId = post.AccountId,
                PostName  = post.PostName,
                Content   = post.Content,
                Address   = post.Address
            };

            res = _rep.CreatePost(postNew);
            return(res);
        }
        public SingleRsp UpdatePost(PostReq post)
        {
            var res        = new SingleRsp();
            var postUpdate = new Posts()
            {
                PostId    = post.PostId,
                AccountId = post.AccountId,
                PostName  = post.PostName,
                Content   = post.Content,
                Address   = post.Address
            };

            res = _rep.UpdatePost(postUpdate);
            return(res);
        }
        public List <PostReq> GetPostsByID(int id)
        {
            List <PostReq> res = new List <PostReq>();
            var            cnn = (SqlConnection)Context.Database.GetDbConnection();

            if (cnn.State == ConnectionState.Closed)
            {
                cnn.Open();
            }
            try
            {
                SqlDataAdapter da  = new SqlDataAdapter();
                DataSet        ds  = new DataSet();
                var            cmd = cnn.CreateCommand();
                cmd.CommandText = "GetPostsByAccountID";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@AccountID", id);
                da.SelectCommand = cmd;
                da.Fill(ds);
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        PostReq p = new PostReq()
                        {
                            PostId     = (int)row["PostId"],
                            AccountId  = (int)row["AccountId"],
                            PostName   = row["PostName"].ToString(),
                            Content    = row["Content"].ToString(),
                            Address    = row["Address"].ToString(),
                            PictureURL = row["PictureURL"].ToString()
                        };
                        res.Add(p);
                    }
                }
            }
            catch (Exception ex)
            {
                res = null;
            }
            return(res);
        }
        public IActionResult DeletePost([FromBody] PostReq req)
        {
            var res = _svc.DeletePost(req.PostId);

            return(Ok(res));
        }
        public IActionResult UpdatePost([FromBody] PostReq req)
        {
            var res = _svc.UpdatePost(req);

            return(Ok(res));
        }
        public IActionResult GetDetailPostByID([FromBody] PostReq req)
        {
            var res = _svc.GetDetailPostByID(req.PostId);

            return(Ok((object)res));
        }
        public bool AddPost(PostReq post)
        {
            bool res = _rep.AddPost(post);

            return(res);
        }