private bool IsValid(SelectedWorker obj, Result <SelectedWorker> result) { if (!ValidationHelper.IsStringValid(obj.UserId.ToString())) { result.HasError = true; result.Message = "Invalid UserId"; return(false); } if (!ValidationHelper.IsStringValid(obj.Price.ToString())) { result.HasError = true; result.Message = "Invalid Price"; return(false); } if (!ValidationHelper.IsStringValid(obj.SubmissionDate.ToString())) { result.HasError = true; result.Message = "Invalid SubmissionDate"; return(false); } return(true); }
public Result <SelectedWorker> Save(SelectedWorker userinfo) { var result = new Result <SelectedWorker>(); try { var objtosave = DbContext.SelectedWorkers.FirstOrDefault(u => u.PostId == userinfo.PostId); if (objtosave == null) { objtosave = new SelectedWorker(); DbContext.SelectedWorkers.Add(objtosave); } objtosave.UserId = userinfo.UserId; objtosave.Price = userinfo.Price; objtosave.SubmissionDate = userinfo.SubmissionDate; if (!IsValid(objtosave, result)) { return(result); } DbContext.SaveChanges(); } catch (Exception ex) { result.HasError = true; result.Message = ex.Message; } return(result); }
//public bool Delete(int id) //{ // var result = new Result<SelectedWorker>(); // try // { // string query = "delete from SelectedWorker where SavedFileId=" + id; // return DataAccess.ExecuteQuery(query) > 0; // } // catch (Exception ex) // { // return false; // } //} private SelectedWorker ConvertToEntity(DataRow row) { try { SelectedWorker u = new SelectedWorker(); u.PostId = Int32.Parse(row["PostId"].ToString()); u.UserId = Int32.Parse(row["UserID"].ToString()); u.Price = Int32.Parse(row["Price"].ToString()); u.SubmissionDate = Convert.ToDateTime(row["SubmissionDate"]); return(u); } catch (Exception) { return(null); } }
public Result <SelectedWorker> Save(SelectedWorker SelectedWorker) { var result = new Result <SelectedWorker>(); try { string query = "select * from SelectedWorker where PostId=" + SelectedWorker.PostId; //var dt = DataAccess.GetDataTable(query); //if (dt == null || dt.Rows.Count == 0) //{ // SelectedWorker.SavedFileId = GetID(); var d = SelectedWorker.SubmissionDate.ToString(string.Format("dd/MMM/yyyy")); query = "insert into SelectedWorker values(" + SelectedWorker.PostId + "," + SelectedWorker.UserId + "," + SelectedWorker.Price + ",'" + d + "')"; //} //else //{ // query = "update SelectedWorker set ProjectName='" + SelectedWorker.ProjectName + "',StartTime='" + SelectedWorker.StartTime + "',EndTime='" + SelectedWorker.EndTime + "',Description='" + SelectedWorker.Description + "',ProjectSection='" + SelectedWorker.ProjectSection + "',Price=" + SelectedWorker.Price + ",Members=" + SelectedWorker.Members + " where PostID=" + SelectedWorker.PostId; //} result.HasError = DataAccess.ExecuteQuery(query) <= 0; if (result.HasError) { result.Message = "Something Went Wrong"; } else { result.Data = SelectedWorker; } } catch (Exception ex) { result.HasError = true; result.Message = ex.Message; } return(result); }
//private int GetID() //{ // string query = "select * from SelectedWorker order by PostId desc"; // var dt = DataAccess.GetDataTable(query); // int id = 1; // if (dt != null && dt.Rows.Count != 0) // id = Int32.Parse(dt.Rows[0]["SavedFileId"].ToString()) + 1; // return id; //} public List <SelectedWorker> GetAll() { var result = new List <SelectedWorker>(); try { string query = "select * from SelectedWorker"; var dt = DataAccess.GetDataTable(query); if (dt != null && dt.Rows.Count != 0) { for (int i = 0; i <= dt.Rows.Count; i++) { SelectedWorker u = ConvertToEntity(dt.Rows[i]); result.Add(u); } } } catch (Exception ex) { return(result); } return(result); }