public ActionResult Create(Project project) { if (!ModelState.IsValid) { var viewModel = new ProjectFormViewModel { Project = project, }; return(View("Create", viewModel)); } if (project.Id == 0) { _context.Projects.Add(project); } else { var projectInDb = _context.Projects.Single(t => project.Id == project.Id); projectInDb.Name = project.Name; projectInDb.Description = project.Description; } _context.SaveChanges(); return(RedirectToAction("List", "Projects")); }
public ActionResult Save(Project project) { if (!this.ModelState.IsValid) { var viewModel = new ProjectFormViewModel(project); return(View("ProjectForm", viewModel)); } var message = ""; if (project.Id == 0) { this._context.Projects.Add(project); message = "created"; } else { var projectInDb = this._context.Projects.Single(p => p.Id == project.Id); projectInDb.Name = project.Name; projectInDb.ProjectInvestment = project.ProjectInvestment; projectInDb.ProjectPrice = project.ProjectPrice; projectInDb.ProjectInfo = project.ProjectInfo; message = "edited"; } this._context.SaveChanges(); this.AddToastMessage("Congratulations", $"Program {project.Name} {message} successfully!", ToastType.Success); return(RedirectToAction("Details", "Programs", new { id = project.ProgramId })); }
public ViewResult ProjectDetails(Guid?id) { if (!id.HasValue) { var defaultViewModel = new ProjectFormViewModel { Groups = _groupService.GetGroup(), EmployeesList = GetAllEmployeesNames(_employeeService.GetEmployee()) }; return(View("ProjectDetails", defaultViewModel)); } else { var project = _projectService.GetProjectWithEmployees(id.Value); IEnumerable <string> visas = DisplayMembers(project); var viewModel = new ProjectFormViewModel { Project = project, EditMode = true, Groups = _groupService.GetGroup(), EmployeesList = GetAllEmployeesNames(_employeeService.GetEmployee()), Members = visas }; return(View("ProjectDetails", viewModel)); } }
// GET: Projects/Create public async Task <IActionResult> Create(int?clients, int?projects, int?manager_project, int?manager) { GetSessions(); if (ViewBag.Email == null) { return(ExpiredSession()); } try { int accessLevel = ViewBag.AcessLevel; int employeeId = ViewBag.Id; ViewBag.Clients = clients; ViewBag.Projects = projects; ViewBag.Manager = manager; ViewBag.Manager_Project = manager_project; var client = await _clientService.FindAllForProjectsAsync(accessLevel, employeeId); var employee = await _employeeService.FindAllManagersAsync(); var viewModel = new ProjectFormViewModel { Client = client, Employee = employee }; return(View(viewModel)); } catch (Exception e) { return(RedirectToAction(nameof(Error), new { message = e.Message })); } }
public void If_Project_UserId_Does_Not_Match_Current_User_Do_Not_Add() { // Spoof an authenticated user by generating a ClaimsPrincipal var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.NameIdentifier, "FIREBASE_USER1"), }, "TestAuthentication")); // Create a new project ProjectFormViewModel projectForm = new ProjectFormViewModel { Project = new Project() { // have a userId coming in that does not match UserId = 666, Name = "New stuff", CreationDate = DateTime.Now - TimeSpan.FromDays(10) }, ProjectCollections = new List <ProjectCollection>() }; // Spoof UserController var controller = new ProjectController(_fakeUserRepo.Object, _fakeProjectRepo.Object, _fakeProjColRepo.Object); controller.ControllerContext = new ControllerContext(); // Required to create the controller controller.ControllerContext.HttpContext = new DefaultHttpContext { User = user }; // Pretend the user is making a request to the controller // Attempt to Get this User's projects var response = controller.Add(projectForm); // Returns Ok Assert.IsType <BadRequestResult>(response); }
public ActionResult CreateOrUpdateForm(ProjectFormViewModel viewModel) { //if ProjectId is 0 means its new created project if (viewModel.Project.Id == 0) { Project project = new Project() { Name = viewModel.Project.Name, Users = userHelper.GetAllUsersFromIds(viewModel.SelectedId), }; projectHelper.AddProject(project); } //Else project need to update else { Project project = new Project() { Id = viewModel.Project.Id, Name = viewModel.Project.Name, Users = userHelper.GetAllUsersFromIds(viewModel.SelectedId), }; projectHelper.UpdateProject(project); } return(RedirectToAction("List")); }
public Project(ProjectFormViewModel viewModel) { DueDate = viewModel.DueDate; StartingDate = DateTime.Now; Description = viewModel.Description.ToString(); DepartmentId = viewModel.DepartmentId; EndDate = viewModel.EndDate; }
public ActionResult CreateProject(ProjectFormViewModel viewModel) { var newProject = new Project(viewModel); _context.Projects.Add(newProject); _context.SaveChanges(); return(RedirectToAction("Index", "Projects")); }
public ActionResult New(int programId) { var defaultClientName = this._context.Clients.FirstOrDefault(c => c.Id == Defaults.ClientId).Name; var defaultConsultantName = this._context.Consultants.FirstOrDefault(c => c.Id == Defaults.ConsultantId).Name; var viewModel = new ProjectFormViewModel(programId, defaultClientName, defaultConsultantName); return(View("ProjectForm", viewModel)); }
//Get All TodayProjects For Today public ActionResult GetTodayProjects(ProjectFormViewModel viewModel) { var indexviewModel = new ProjectFormViewModel { Departments = _context.Departments.ToList() }; return(View(indexviewModel)); }
//Create Get public ActionResult CreateProject() { var viewModel = new ProjectFormViewModel { Departments = _context.Departments.ToList() }; return(View(viewModel)); }
public ActionResult TodayProjectsByDepartment(ProjectFormViewModel viewModel) { var projects = _context.Projects .Include(p => p.Department) .Where(p => p.EndDate == null && p.StartingDate <= DateTime.Now && p.DepartmentId == viewModel.DepartmentId && p.EndDate == null) .ToList(); return(View(projects)); }
// GET: Projects/Create public ActionResult Create() { var viewModel = new ProjectFormViewModel { Project = new Project(), }; return(View("Create", viewModel)); }
public ActionResult New() { var viewModel = new ProjectFormViewModel { Categories = _context.Categories.ToList() }; return(View("ProjectForm", viewModel)); }
public ActionResult Create() { ViewBag.Action = "Create"; ProjectFormViewModel viewModel = new ProjectFormViewModel() { Project = new Project(), UsersList = new SelectList(db.Users.ToList(), "Id", "UserName") }; return(View("CreateOrUpdateForm", viewModel)); }
public void AddNewProject(string name, string prefix) { var projectViewModel = new ProjectFormViewModel() { Name = name, Prefix = prefix }; var project = _mapper.Map <ProjectFormViewModel, Project>(projectViewModel); _projectRepository.Add(project); _projectRepository.Save(); }
public ActionResult Update(int id) { ViewBag.Action = "Update"; ProjectFormViewModel viewModel = new ProjectFormViewModel() { Project = projectHelper.GetProject(id), UsersList = new SelectList(db.Users.ToList(), "Id", "UserName"), SelectedId = projectHelper.GetProjectUserIds(id) }; return(View("CreateOrUpdateForm", viewModel)); }
public void User_Can_Not_Update_Projects_With_Duplicate_Names() { // Make a new project to pass in to update var projectForm = new ProjectFormViewModel { Project = new Project() { Id = 2, UserId = 1, Name = "It", CreationDate = DateTime.Now - TimeSpan.FromDays(15) }, ProjectCollections = new List <ProjectCollection>() { new ProjectCollection() { ProjectId = 2, // I won't know this until it's made CollectionId = 2 }, new ProjectCollection() { ProjectId = 2, // I won't know this until it's made CollectionId = 1 } } }; // Spoof an authenticated user by generating a ClaimsPrincipal var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.NameIdentifier, "FIREBASE_ID_1"), }, "TestAuthentication")); // Instantiate a real repos var projectRepo = new ProjectRepository(_context); var userRepo = new UserRepository(_context); var projColRepo = new ProjectCollectionRepository(_context); // Instantiate a real ProjectController, passing in ProjectRepo var controller = new ProjectController(userRepo, projectRepo, projColRepo); controller.ControllerContext = new ControllerContext(); // Required to create the controller controller.ControllerContext.HttpContext = new DefaultHttpContext { User = user }; // Pretend the user is making a request to the controller // Attempt to Update project var response = controller.Put(projectForm.Project.Id, projectForm); // Should return created result Assert.IsType <NotFoundResult>(response); }
public ActionResult Edit(int id) { var project = this._context.Projects.Include(p => p.Client).Include(p => p.Consultant).SingleOrDefault(p => p.Id == id); if (project == null) { return(HttpNotFound()); } var viewModel = new ProjectFormViewModel(project); return(View("ProjectForm", viewModel)); }
private ActionResult CatchException(ProjectFormViewModel viewModel) { if (!viewModel.EditMode) { ModelState.Remove("Project.ID"); } var errViewModel = new ProjectFormViewModel { Groups = _groupService.GetGroup(), Project = viewModel.Project, Members = viewModel.Members, EditMode = viewModel.EditMode, EmployeesList = GetAllEmployeesNames(_employeeService.GetEmployee()) }; //Step1: Check for data annotation attribute client side validation if (!ModelState.IsValid) { return(View("ProjectDetails", errViewModel)); } try { if (viewModel.EditMode) { _projectService.Update(viewModel.Project, viewModel.Members); } else { _projectService.Create(viewModel.Project, viewModel.Members); } } catch (BusinessException ex) { errViewModel.Exception = ex; ModelState.AddModelError(string.Empty, ex.Message); return(View("ProjectDetails", errViewModel)); } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); errViewModel.Exception = ex; return(View("ProjectDetails", errViewModel)); } return(RedirectToAction("Index", "Projects")); }
public ActionResult Edit(int id) { var project = _context.Projects.SingleOrDefault(t => t.Id == id); if (project == null) { return(HttpNotFound()); } var viewModel = new ProjectFormViewModel { Project = project, }; return(View("Create", viewModel)); }
public ProjectWindow(IViewManager viewManager) { InitializeComponent(); //ProjectTabs.ItemContainerStyle = this.FindResource("Right90") as Style; ProjectFormViewModel vm = (ProjectFormViewModel)this.DataContext; // this creates an instance of the ViewModel if (vm.CloseAction == null) { vm.CloseAction = new Action(() => this.Close()); } if (vm.OpenTab == null) { vm.OpenTab = new Action(() => this.ProjectTabs.SelectedIndex = 0); } vm.ViewManager = viewManager; }
public dynamic ProjectSave() { ProjectFormViewModel model = this.Bind <ProjectFormViewModel>(); ProjectModel project = Mapper.Map <ProjectFormViewModel, ProjectModel>(model); ValidationResult validationResult = _projectValidator.Validate(project); if (validationResult.Success) { _dbContext.BeginTransaction(); project = _createProjectCommand.Execute(project); _dbContext.Commit(); } SaveResultModel result = new SaveResultModel(project.Id.ToString(), validationResult.Success, validationResult.Messages.ToArray()); return(this.Response.AsJson(result)); }
public ActionResult Edit(int id) { var project = _context.Projects.Include(p => p.Category).SingleOrDefault(p => p.Id == id); if (project == null) { return(HttpNotFound()); } var viewModel = new ProjectFormViewModel { Project = project, Categories = _context.Categories.ToList() }; return(View("ProjectForm", viewModel)); }
// GET: Projects public async Task <IActionResult> Index(int?accessLevel, int?employeeId, int?clients, int?projects, int?manager_project, int?manager) { GetSessions(); if (ViewBag.Email == null) { return(ExpiredSession()); } try { accessLevel = ViewBag.AcessLevel; employeeId = ViewBag.Id; ViewBag.Clients = clients; ViewBag.Projects = projects; ViewBag.Manager = manager; ViewBag.Manager_Project = manager_project; var Listprojects = await _projectService.FindAllToListAsync(accessLevel, employeeId, clients, projects, manager_project, manager); var clientes = await _clientService.FindAllForProjectsAsync(accessLevel, employeeId); var projetos = await _projectService.FindProjectAsync((int)accessLevel, (int)employeeId); var funcionarios = await _employeeService.FindAllManagersAsync(); var viewModel = new ProjectFormViewModel { ListProject = Listprojects, Client = clientes, Projects = projetos, Employee = funcionarios }; return(View(viewModel)); } catch (Exception e) { return(RedirectToAction(nameof(Error), new { message = e.Message })); } }
public void If_Project_To_Update_Is_Not_Mine_Do_Not_Update() { // Spoof an authenticated user by generating a ClaimsPrincipal var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.NameIdentifier, "FIREBASE_USER2"), }, "TestAuthentication")); // Make a fake project to update ProjectFormViewModel projectForm = new ProjectFormViewModel { Project = new Project() { Id = 1, UserId = 1, Name = "New PROJECT!!!!", CreationDate = DateTime.Now - TimeSpan.FromDays(10) }, ProjectCollections = new List <ProjectCollection>() }; // Use a matching Id var projectParamId = 1; // Spoof UserController var controller = new ProjectController(_fakeUserRepo.Object, _fakeProjectRepo.Object, _fakeProjColRepo.Object); controller.ControllerContext = new ControllerContext(); // Required to create the controller controller.ControllerContext.HttpContext = new DefaultHttpContext { User = user }; // Pretend the user is making a request to the controller // Attempt to Get this User's projects var response = controller.Put(projectParamId, projectForm); // Returns Ok Assert.IsType <NotFoundResult>(response); }
public ActionResult Save(ProjectFormViewModel projectFormViewModel) { if (!ModelState.IsValid) { return(View("AddProject", projectFormViewModel)); } var path = "~/images/" + Path.GetFileName(projectFormViewModel.File.FileName); var project = new Project { Name = projectFormViewModel.Name, Description = projectFormViewModel.Description, Image = path }; _context.Projects.Add(project); projectFormViewModel.File.SaveAs(Server.MapPath(path)); _context.SaveChanges(); return(View("index")); }
public void Anonymous_User_Can_Not_Add_Project() { // Spoof an authenticated user by generating a ClaimsPrincipal var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.NameIdentifier, "FIREBASE_USER666"), }, "TestAuthentication")); // Create a new project ProjectFormViewModel projectForm = new ProjectFormViewModel { Project = new Project() { Id = 1, UserId = 1, Name = "New stuff", CreationDate = DateTime.Now - TimeSpan.FromDays(10) }, ProjectCollections = new List <ProjectCollection>() }; // Spoof UserController var controller = new ProjectController(_fakeUserRepo.Object, _fakeProjectRepo.Object, _fakeProjColRepo.Object); controller.ControllerContext = new ControllerContext(); // Required to create the controller controller.ControllerContext.HttpContext = new DefaultHttpContext { User = user }; // Pretend the user is making a request to the controller // Attempt to Get this User's projects var response = controller.Add(projectForm); // Returns Ok Assert.IsType <NotFoundResult>(response); // Verify we never called the repo method _fakeProjectRepo.Verify(r => r.Add(It.IsAny <Project>()), Times.Never()); }
public IActionResult Add(ProjectFormViewModel projectForm) { // For the Add, do not need to check for if the projectCollections are in the db // because this Project is unique, there can be no duplicates. var firebaseUser = _utils.GetCurrentUser(User); // Check to ensure an unauthorized user (anonymous account) can not add a project if (firebaseUser == null) { return(NotFound()); } // Ensure the userId on the incoming project matches the person making the request if (projectForm.Project.UserId != firebaseUser.Id) { return(BadRequest()); } // Get all of this user's projects var allProjects = _projectRepo.Get(firebaseUser.Id); // see if the name of the incoming collection is in the db var projectWithThatName = allProjects.Find(c => c.Name == projectForm.Project.Name); // if there is a returned project, we can't add because name isn't unique for this user if (projectWithThatName != null) { return(NotFound()); } // Need to add the default requirement for the project here projectForm.Project.CreationDate = DateTime.Now; try { _projectRepo.Add(projectForm.Project); try { // After we add the project, assign the project id to each projectCollection foreach (var projectCollection in projectForm.ProjectCollections) { projectCollection.ProjectId = projectForm.Project.Id; } } // The user attempted to enter Null for their ProjectCollecitons catch (NullReferenceException e) { // Make a CollectionDetailsViewModel to pass the created collection into for deletion var projectDetails = new ProjectDetailsViewModel { Project = projectForm.Project, ProjectCollections = new List <ProjectCollection>() }; // Remove the just entered collection from db _projectRepo.Delete(projectDetails); // Return a BadRequest return(BadRequest()); } // Add ProjectCollections _projColRepo.Add(projectForm.ProjectCollections); return(Ok(projectForm)); } catch (DbUpdateException e) { return(NotFound()); } }
public IActionResult Put(int id, ProjectFormViewModel incomingProjectForm) { // Get current user var firebaseUser = _utils.GetCurrentUser(User); // Ensure an unauthorized user (anonymous account) can not update if (firebaseUser == null) { return(NotFound()); } // Project Id coming from URL must match the Project object's Id if (id != incomingProjectForm.Project.Id) { return(BadRequest()); } // Get Project by Id to ensure it's in db ProjectDetailsViewModel projectDetailsToUpdate; try { // If a user attempts to get an Id not in the db, causes a NullReferenceException error projectDetailsToUpdate = _projectRepo.GetByProjectId(id); } catch (NullReferenceException e) { return(NotFound()); } // If it wasn't in the db don't let them update if (projectDetailsToUpdate == null) { return(NotFound()); } // Get all of this user's projects var allProjects = _projectRepo.Get(firebaseUser.Id); // see if the name of the incoming collection is in the db var projectWithThatName = allProjects.Where(c => c.Name == incomingProjectForm.Project.Name).ToList(); // If the count is greater than 1, so it's in the db, check to see what the Id is if (projectWithThatName.Count > 0) { // If the Ids match, we can update, otherwise, it's already in db and not the current item if (projectWithThatName[0].Id != incomingProjectForm.Project.Id) { return(NotFound()); } } // Get Project's owner to ensure this is current user's project var projectOwnerId = incomingProjectForm.Project.UserId; // Check if incoming user is the same one requesting deletion if (projectOwnerId != firebaseUser.Id) { return(NotFound()); } // ** At this point, we know the person is able to update the project. // By using the projectDetailsToUpdate we retrieved from the db, // we re-assign its values that are editable, based on the incoming project projectDetailsToUpdate.Project.Name = incomingProjectForm.Project.Name; try { // When updating a Project, we DELETE all current ProjCols then ADD all incoming // Delete all the ProjectCollections from collectionToUpdate _projColRepo.Delete(projectDetailsToUpdate.ProjectCollections); // Add all incoming ProjectCollections _projColRepo.Add(incomingProjectForm.ProjectCollections); _projectRepo.Update(projectDetailsToUpdate.Project); return(NoContent()); } catch (DbUpdateException e) { return(Unauthorized()); // When I get this error message in frontend, tell user the Name was already in db } }