/// <summary> /// Get project by id. /// </summary> /// <param name="context">Context of the database.</param> /// <param name="id">Id of the project to retrieve.</param> /// <returns>A DTO Project corresponding to the "id".</returns> public DTOProject getProjectById(IDbContext context, DTOProject project) { // Get project by id var projectEntity = getById <Project>(context, Mapper.mapProjectEntity(project)).FirstOrDefault(); return(Mapper.mapProjectDTO(projectEntity)); }
/// <summary> /// Create a project and save changes of the context. /// </summary> /// <param name="context">Context of the database.</param> /// <param name="project">ProjectDTO.</param> public int createProjectAndSaveChanges(IDbContext context, DTOProject project) { // Get entity from DTO var projectEntity = Mapper.mapProjectEntity(project); return(insertAndSaveChanges <Project>(context, projectEntity)); }
public int saveProject(DTOProject project) { cmd.Connection = con; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "prc_GuardarProyecto"; cmd.Parameters.AddWithValue("@cod_proyecto", project.Project_Code); cmd.Parameters.AddWithValue("@nom_proyecto", project.Project_Name); cmd.Parameters.AddWithValue("@desc_proyecto", project.Project_Description); try { if (verifyProject(project.Project_Code) == true) { return(3); } else { con.Open(); cmd.ExecuteNonQuery(); return(1); } } catch (Exception ex) { Console.WriteLine("Error" + ex.Message); con.Close(); return(0); } }
public DTOProject insertAndgetProject(DTOProject project) { int idOfInsertedRecord = Container.createIProjectRepository().createProjectAndSaveChanges(Container.createIDbContext(), project); project.Id = idOfInsertedRecord; return(project); }
/// <summary> /// Update a project. /// </summary> /// <param name="context">Context of the database.</param> /// <param name="project">Project DTO.</param> public void updateProject(IDbContext context, DTOProject project) { // Get old entity var oldProjectEntity = context.Projects.Where(x => x.Id == project.Id).FirstOrDefault <Project>(); // Get new entity and keep the dates unchanged var newProjectEntity = Mapper.mapProjectEntity(project); update <Project>(context, newProjectEntity, oldProjectEntity); }
/// <summary> /// If name already exist, an error is added to the modelstate. /// </summary> /// <param name="context">Context of the database.</param> /// <param name="project">A project object.</param> private void checkIfNameAlreadyExist(DTOProject project) { // Check if name already exists bool nameAlreadyExist = projectsRepository.getProjectByName(DbContext, project.Name) != null; if (nameAlreadyExist) { ControllerUI.ModelStateService.AddModelError("Name", GlobalResources["nameEntity"]); } }
public DataTable listProjects(DTOProject project) { cmd.Connection = con; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "prc_ListarProyectos"; DataTable data = new DataTable(); SqlDataAdapter sda = new SqlDataAdapter(cmd); con.Open(); sda.Fill(data); con.Close(); return(data); }
/// <summary> /// Get a project by all fields except the id. /// </summary> /// <param name="context">Context of the database.</param> /// <param name="project">Project to filter by.</param> /// <returns>A collection that match the criteria.</returns> public List <DTOProject> getProjectByAllFieldsExceptId(IDbContext context, DTOProject project) { var query = context.Projects .Where(x => x.Description == project.Description && x.Name == project.Name && x.IsCompleted == project.IsCompleted ).AsEnumerable().Select(x => Mapper.mapProjectDTO(x) ).ToList(); return(query); }
/// <summary> /// Check if the project can be deleted. /// </summary> /// <param name="project">Project to be checked.</param> /// <returns>True if project can be delted. Otherwise false.</returns> private bool projectToDeleteIsOk(DTOProject project) { // Add error if there is requirements var requirementsExist = requirementRepository.requirementExistsByProjectId(DbContext, new DTORequirements { ProjectId = project.Id }); if (requirementsExist) { ControllerUI.ModelStateService.AddModelError(SummaryError, GlobalResources["deleteProject"]); } return(ControllerUI.ModelStateService.IsValid); }
public DTOProject getRandomProject() { string randonStr = getRString().ToString(); var randonCreationDate = getRandomDate(); var projectToInsert = new DTOProject { Description = "Test" + randonStr.ToString(), Name = randonStr, IsCompleted = true, CreationDate = randonCreationDate, FinalizationDate = (DateTime)SqlDateTime.MaxValue }; return(projectToInsert); }
/// <summary> /// Remove a project. /// </summary> /// <param name="project">DTO project to be removed.</param> public void deleteProject(DTOProject project) { using (DbContext) { // Check if the project can be delted if (!projectToDeleteIsOk(project)) { return; } // Delete project and save changes projectsRepository.deleteProject(DbContext, project); DbContext.SaveChanges(); } }
public DTORequirements getRandomRequirement(DTOProject project) { var randonName = "req-" + getRString().ToString(); var randonDescription = "desp-" + getRString().ToString(); var randonCreationDate = getRandomDate(); var requirementToInsert = new DTORequirements { ProjectId = project.Id, Name = randonName, Description = randonDescription, CreationDate = randonCreationDate, FinalizationDate = (DateTime)SqlDateTime.MaxValue }; return(requirementToInsert); }
/// <summary> /// Update a project. /// </summary> /// <param name="project">Project object that contains the new information to be updated.</param> public void updateProject(DTOProject project) { using (DbContext) { // Validate the project if (!projectToUpdateIsOk(project)) { return; } // Set dates var oldProject = projectsRepository.getProjectById(DbContext, project); project.CreationDate = oldProject.CreationDate; project.FinalizationDate = oldProject.FinalizationDate; // Update project projectsRepository.updateProject(DbContext, project); DbContext.SaveChanges(); } }
public int deleteProject(DTOProject project) { cmd.Connection = con; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "prc_EliminarProyecto"; cmd.Parameters.AddWithValue("@cod_proyecto", project.Project_Code); try { con.Open(); cmd.ExecuteNonQuery(); return(1); } catch (Exception ex) { Console.WriteLine("Error" + ex.Message); con.Close(); return(0); } }
/// <summary> /// Create project. /// </summary> /// <param name="project">DTO Project to create.</param> public int createProject(DTOProject project) { using (DbContext) { // Validate the project if (!projectToInsertIsOk(project)) { return(-1); } // Set the date fields project.CreationDate = DateTime.Now; project.FinalizationDate = (DateTime)SqlDateTime.MinValue; // Create project int id = projectsRepository.createProjectAndSaveChanges(DbContext, project); // Return id return(id); } }
public void UpdateProject_updates_projects_and_return_json() { // Old Project var oldProject = Container.createIProjectRepository().getAllProjects(Container.createIDbContext()).FirstOrDefault(); // New project var randonDescription = "DescUp-" + r.Next(1000).ToString(); var randonName = "NamUp-" + r.Next(1000).ToString(); var randonIsComplete = r.Next(0, 1) == 1; var newProject = new DTOProject { Id = oldProject.Id, // <-- Id of the old project Description = randonDescription, Name = randonDescription, IsCompleted = randonIsComplete, CreationDate = (DateTime)SqlDateTime.MinValue, FinalizationDate = (DateTime)SqlDateTime.MaxValue }; // Arrange var projectsController = Container.createProjectsController(); // Act var result = projectsController.UpdateProject(newProject) as JsonResult; // Assert var expectedOldRecord = Container.createIProjectRepository().getProjectByAllFieldsExceptId(Container.createIDbContext(), oldProject); var expectedNewRecord = Container.createIProjectRepository().getProjectByAllFieldsExceptId(Container.createIDbContext(), newProject); Assert.IsTrue(result != null, "It is not json result."); Assert.IsTrue(expectedOldRecord.Count == 0 && expectedNewRecord.Count >= 1, "The record was not updated."); Assert.IsTrue ( expectedNewRecord.FirstOrDefault().Description == newProject.Description && expectedNewRecord.FirstOrDefault().Name == newProject.Name, "There record was updated successfully." ); }
// Requirement public List <DTORequirements> insertAndGetRequirementCollection(int numberOfRequirmentsToCreate, DTOProject project) { // Ge requirements collection List <DTORequirements> requirementCollection = new List <DTORequirements>(); for (int i = 0; i < numberOfRequirmentsToCreate; i++) { var requirementToInsert = getRandomRequirement(project); requirementCollection.Add(requirementToInsert); } // Insert requirements using (var context = Container.createIDbContext()) { foreach (var requirement in requirementCollection) { int id = Container.createIRequirementsRepository().insertRequirementAndSaveChanges(context, requirement); requirement.Id = id; } } return(requirementCollection); }
/// <summary> /// Check if the project to insert is ok. /// </summary> /// <param name="context">Context of the database.</param> /// <param name="project">Project to be inserted.</param> /// <returns>True if project to insert is ok, otherwise false.</returns> private bool projectToUpdateIsOk(DTOProject project) { checkIfNameAlreadyExist(project); return(ControllerUI.ModelStateService.IsValid); }
/// <summary> /// Map project and DTO project entity to DTO. /// </summary> /// <param name="dtoProject">DTO project to map from.</param> /// <returns>A project entity.</returns> public Project mapProjectEntity(DTOProject dtoProject) { return(map <Project>(dtoProject)); }
/// <summary> /// Delete a project. /// </summary> /// <param name="context">Context of the database.</param> /// <param name="project">Project DTO.</param> public void deleteProject(IDbContext context, DTOProject project) { delete <Project>(context, Mapper.mapProjectEntity(project)); }
public ActionResult CreateProject([Bind(Exclude = "Id")] DTOProject project) { int id = projectService.createProject(project); return(Json(id)); }
public ActionResult UpdateProject(DTOProject project) { projectService.updateProject(project); return(Json(true)); }
public ActionResult DeleteProject(DTOProject project) { projectService.deleteProject(project); return(Json(true)); }