// CREATE & UPDATE public Project Save(Project project) { string query = @"INSERT INTO Project ( ProjectName, ProjectDescription, ImageUrl, CurrentFund, Goal, StartDate, EndDate, CreatorID, CategoryID ) VALUES ( @ProjectName, @ProjectDescription, @ImageUrl, @CurrentFund, @Goal, @StartDate, @EndDate, @CreatorID, @CategoryID )"; using (SqlConnection db = new SqlConnection(CONN_STR)) { if (db.State != System.Data.ConnectionState.Open) { db.Open(); } using (SqlCommand cmd = db.CreateCommand()) { cmd.CommandText = query; cmd.Parameters.AddWithValue("ProjectName", project.ProjectName); cmd.Parameters.AddWithValue("ProjectDescription", project.ProjectDescription); cmd.Parameters.AddWithValue("ImageUrl", project.ImageUrl); cmd.Parameters.AddWithValue("CurrentFund", project.CurrentFund); cmd.Parameters.AddWithValue("Goal", project.Goal); cmd.Parameters.AddWithValue("StartDate", project.StartDate); cmd.Parameters.AddWithValue("EndDate", project.EndDate); cmd.Parameters.AddWithValue("CreatorID", project.Creator != null ? project.Creator.UserID : 0); cmd.Parameters.AddWithValue("CategoryID", project.Category != null ? project.Category.CategoryID : 0); cmd.ExecuteNonQuery(); cmd.CommandText = "SELECT @@Identity"; project.ProjectID = (int) (decimal) cmd.ExecuteScalar(); } } return project; }
public ActionResult New(ProjectForm projectForm) { if (ModelState.IsValid) { Project newProject = new Project(); newProject.ProjectName = projectForm.ProjectName; newProject.ProjectDescription = projectForm.ProjectDescription; newProject.CurrentFund = 0; newProject.Goal = projectForm.Goal; newProject.StartDate = DateTime.Now; newProject.EndDate = newProject.StartDate + TimeSpan.FromDays(projectForm.FundingLength); int CatID = int.Parse(projectForm.SelectedCategory); newProject.Category = CategoryRepository.Find(CatID); newProject.Creator = (User)Session["CurrentUser"]; newProject.ImageUrl = projectForm.ImageUrl; newProject = ProjectRepository.Save(newProject); projectForm.ProjectID = newProject.ProjectID; return View("NewBis", projectForm); } else { List<SelectListItem> TempList = new List<SelectListItem>(); foreach (Category c in CategoryRepository.FindAll()) { TempList.Add( new SelectListItem { Value = c.CategoryID.ToString(), Text = c.CategoryName, Selected = false, Disabled = false, Group = null } ); } projectForm.Categories = TempList; return View(projectForm); } }
public List<Project> FindAll() { List<Project> Projects = new List<Project>(); string query = "SELECT * FROM Project"; using (SqlConnection db = new SqlConnection(CONN_STR)) { if (db.State != System.Data.ConnectionState.Open) { db.Open(); } using (SqlCommand cmd = db.CreateCommand()) { cmd.CommandText = query; using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { Project p = new Project(); p.ProjectID = reader.GetInt32(0); p.ProjectName = reader.GetString(1); p.ProjectDescription = reader.GetString(2); p.ImageUrl = reader.GetString(3); p.CurrentFund = reader.GetInt32(4); p.Goal = reader.GetInt32(5); p.StartDate = reader.GetDateTime(6); p.EndDate = reader.GetDateTime(7); IUserRepository UserRepository = new UserRepository(); int userID = reader.GetInt32(reader.GetOrdinal("CreatorID")); p.Creator = UserRepository.Find(userID); ICategoryRepository CategoryRepository = new CategoryRepository(); int catID = reader.GetInt32(reader.GetOrdinal("CategoryID")); p.Category = CategoryRepository.Find(catID); IBackingRepository BackingRepository = new BackingRepository(); p.CurrentFund = BackingRepository.FindAllForProject(p.ProjectID).Sum(b => b.BackingAmount); Projects.Add(p); } } } } return Projects; }
public void Delete(Project project) { string query = "DELETE FROM Project"; using (SqlConnection db = new SqlConnection(CONN_STR)) { if (db.State != System.Data.ConnectionState.Open) { db.Open(); } using (SqlCommand cmd = db.CreateCommand()) { cmd.CommandText = query; cmd.ExecuteNonQuery(); } } }