public PostsBEL AddNewPost(PostsBEL postBEL) { PostsBEL PostDetails = new PostsBEL(); DataSet ds = null; try { SqlParameter[] SqlParms = new SqlParameter[6]; SqlParms[0] = new SqlParameter("@UserId", SqlDbType.NVarChar, 50); SqlParms[0].Direction = ParameterDirection.Input; SqlParms[0].Value = postBEL.userId; SqlParms[1] = new SqlParameter("@PostText", SqlDbType.NVarChar, 1500); SqlParms[1].Direction = ParameterDirection.Input; SqlParms[1].Value = postBEL.postDescription; SqlParms[2] = new SqlParameter("@PostName", SqlDbType.NVarChar, 500); SqlParms[2].Direction = ParameterDirection.Input; SqlParms[2].Value = postBEL.postName; SqlParms[3] = new SqlParameter("@Avatar", SqlDbType.NVarChar, 800); SqlParms[3].Direction = ParameterDirection.Input; SqlParms[3].Value = postBEL.avatar; SqlParms[4] = new SqlParameter("@Error", SqlDbType.NVarChar, 150); SqlParms[4].Direction = ParameterDirection.Output; SqlParms[5] = new SqlParameter("@PostID", SqlDbType.NVarChar, 20); SqlParms[5].Direction = ParameterDirection.Output; ds = SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, "Add_NewPost", SqlParms); //&& Convert.ToString(SqlParms[5].Value) != "0" if (ds == null && ds.Tables.Count <= 0) { PostDetails.postName = ""; PostDetails.postId = "0"; PostDetails.ErrorMessage = Convert.ToString(SqlParms[4].Value) + " Data Not Found"; } else if (Convert.ToString(SqlParms[4].Value) == string.Empty) { PostDetails.postId = ds.Tables[0].Rows[0]["Post_ID"].ToString(); PostDetails.userId = ds.Tables[0].Rows[0]["User_Id"].ToString(); PostDetails.postDescription = ds.Tables[0].Rows[0]["Text"].ToString(); PostDetails.postName = ds.Tables[0].Rows[0]["PostName"].ToString(); PostDetails.avatar = ds.Tables[0].Rows[0]["Avatar"].ToString(); PostDetails.likes = "0"; PostDetails.ErrorMessage = string.Empty; // ResultDTO. = Convert.ToString(SqlParms[5].Value); } else { PostDetails.postName = ""; PostDetails.postId = "0"; PostDetails.ErrorMessage = Convert.ToString(SqlParms[4].Value); } } catch (Exception ex) { PostDetails.postName = ""; PostDetails.postId = "0"; PostDetails.ErrorMessage = "DataBase Error Occured : " + ex.Message; } return(PostDetails); }
public List <PostsBEL> GetAllPosts() { List <PostsBEL> Posts = new List <PostsBEL>(); try { DataSet ds = null; SqlParameter[] SqlParms = new SqlParameter[1]; SqlParms[0] = new SqlParameter("@Error", SqlDbType.NVarChar, 150); SqlParms[0].Direction = ParameterDirection.Output; //SqlParms[7] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20); //SqlParms[7].Direction = ParameterDirection.Output; ds = SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, "Get_AllPosts", SqlParms); if (ds != null && ds.Tables[0].Rows.Count > 0) { CommentsBEL c = new CommentsBEL(); /*Posts = (from DataRow Post in ds.Tables[0].Rows * * select new PostsBEL * { * /* userId = Convert.ToString(parkingrow["User_id"]), * postId = Convert.ToString(parkingrow["Menu_id"]), * postDescription = Convert.ToString(parkingrow["Menu_description"]), * * post.a Comments * * Success = true, * ErrorMessage = "", * } * ).ToList<PostsBEL>(); */ PostsBEL post; // = new PostsBEL(); List <CommentsBEL> comments; // = new List<CommentsBEL>(); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { post = new PostsBEL(); comments = new List <CommentsBEL>(); post.postId = ds.Tables[0].Rows[i]["Post_id"].ToString(); post.postName = ds.Tables[0].Rows[i]["PostName"].ToString(); post.postDescription = ds.Tables[0].Rows[i]["PostDescription"].ToString(); post.postedByUName = ds.Tables[0].Rows[i]["postedby"].ToString(); post.userId = ds.Tables[0].Rows[i]["postedByUid"].ToString(); post.dateCreated = ds.Tables[0].Rows[i]["PostCreatedOn"].ToString(); post.avatar = ds.Tables[0].Rows[i]["PostAvatar"].ToString(); post.likes = ds.Tables[0].Rows[i]["likes"].ToString(); post.Success = true; //c = new CommentsBEL(); for (int k = i; k < ds.Tables[0].Rows.Count; k++) { if (post.postId == ds.Tables[0].Rows[k]["CPostId"].ToString()) { c = new CommentsBEL(); c.commentId = ds.Tables[0].Rows[k]["Comment_id"].ToString(); c.commentText = ds.Tables[0].Rows[k]["comment"].ToString(); c.CommentedByUName = ds.Tables[0].Rows[k]["CommentedByName"].ToString(); c.userId = ds.Tables[0].Rows[k]["commentedByUid"].ToString(); c.likes = ds.Tables[0].Rows[k]["Commentlikes"].ToString(); c.avatar = ds.Tables[0].Rows[k]["CommentAvatar"].ToString(); c.dateCreated = ds.Tables[0].Rows[k]["CommentDate"].ToString(); c.postId = ds.Tables[0].Rows[k]["CPostid"].ToString(); comments.Add(c); i = k; } } post.Comments = comments; // i = k; //po Posts.Add(post); } return(Posts); } else { Posts.Add(new PostsBEL() { Success = false, ErrorMessage = Convert.ToString(SqlParms[1].Value) }); return(Posts); } } catch (Exception ex) { Posts.Add(new PostsBEL() { Success = false, ErrorMessage = Convert.ToString(ex.Message.ToString()) }); return(Posts); } // return AvailableParkings; }
public HttpResponseMessage AddPost(AddPostViewModel vm) { var res = new HttpResponseMessage(); JObject jContent = new JObject(); Random ran = new Random(); if (vm != null) { // vm.Password = Secure.Encrypt(vm.Password);// Secure.Encrypt("TWuser@" + Convert.ToString(ran.Next(1000, 9999))); // vm.ConfirmPassword = Secure.Encrypt(vm.Password); if (ModelState.IsValid) { try { PostsBEL objPostBEL = new PostsBEL(); objPostBEL.userId = vm.UserId; objPostBEL.postName = vm.PostName; objPostBEL.postDescription = vm.PostDescription; objPostBEL.avatar = vm.Avatar; // objPostBEL. = vm.Mobile; UserDetailsDAL objUserDAL = new UserDetailsDAL(); PostsBEL ulvm = new PostsBEL(objUserDAL.AddNewPost(objPostBEL)); if (ulvm.postId != "0") { res.StatusCode = HttpStatusCode.Created; jContent = new JObject( new JProperty("result", new JObject( new JProperty("Success", true), new JProperty("Message", "Post added successfully"), new JProperty("PostId", ulvm.postId)))); } else { res.StatusCode = HttpStatusCode.BadRequest; jContent = new JObject( new JProperty("result", new JObject( new JProperty("Success", false), new JProperty("Message", ulvm.ErrorMessage), new JProperty("ErrorMessage", ulvm.ErrorMessage), new JProperty("PostId", ulvm.postId)))); } } catch (Exception ex) { res.StatusCode = HttpStatusCode.BadRequest; jContent = new JObject( new JProperty("result", new JObject( new JProperty("Success", false), new JProperty("Message", ex.Message), new JProperty("ErrorMessage", ex.InnerException.ToString()), new JProperty("PostId", null)))); } } else { var errorList = (from item in ModelState where item.Value.Errors.Any() select item.Value.Errors[0].ErrorMessage).ToList(); res.StatusCode = HttpStatusCode.BadRequest; jContent = new JObject( new JProperty("result", new JObject( new JProperty("Success", false), new JProperty("Message", "Invalid Input"), new JProperty("ErrorMessage", errorList), new JProperty("PostId", null)))); } } res.Content = new StringContent(jContent.ToString()); return(res); }