public void DTO2DB_Company(DTO.CompanyDTO dtoCompany, ref Company dbCompany) { AutoMapper.Mapper.Map <DTO.CompanyDTO, Company>(dtoCompany, dbCompany); // Branch foreach (Branch dbBranch in dbCompany.Branch.ToList()) { if (!dtoCompany.Branches.Select(s => s.BranchID).Contains(dbBranch.BranchID)) { dbCompany.Branch.Remove(dbBranch); } } foreach (DTO.BranchDTO dtoBranch in dtoCompany.Branches.ToList()) { Branch dbBranch; if (dtoBranch.BranchID <= 0) { dbBranch = new Branch(); dbCompany.Branch.Add(dbBranch); } else { dbBranch = dbCompany.Branch.FirstOrDefault(o => o.BranchID == dtoBranch.BranchID); } if (dbBranch != null) { AutoMapper.Mapper.Map <DTO.BranchDTO, Branch>(dtoBranch, dbBranch); } } }
/// <summary> /// Récupérer tous les congés dans la base de données /// </summary> /// <returns></returns> public override IList <DTO.LeaveDTO> GetAll() { try { IDbConnection connexion = new SqlConnection(connectionString); IDbCommand commande = connexion.CreateCommand(); string query = "SELECT * FROM Leave"; commande.CommandText = query; commande.CommandType = CommandType.Text; connexion.Open(); IDataReader dataReader = commande.ExecuteReader(); IList <DTO.LeaveDTO> leaves = null; DTO.CompanyDTO companyDao = null; while (dataReader.Read()) { DTO.LeaveDTO leaveDao = new DTO.LeaveDTO(); leaveDao.ID = (int)dataReader["ID"]; leaveDao.StartDate = ((DateTimeOffset)dataReader["StartDate"]).DateTime; if (dataReader["EndDate"] != DBNull.Value) { leaveDao.EndDate = ((DateTimeOffset)dataReader["EndDate"]).DateTime; } leaveDao.Description = (string)dataReader["Description"]; int companyId = (int)dataReader["Company_ID"]; companyDao = DAO.CompanyDAO.Instance.GetById(companyId); leaveDao.Company = companyDao; leaves = leaves ?? new List <DTO.LeaveDTO>(); leaves.Add(leaveDao); } connexion.Close(); connexion.Dispose(); return(leaves); } catch (Exception) { throw; } }
/// <summary> /// Récupérer un congé par ID /// </summary> /// <param name="Id"></param> /// <returns></returns> public override DTO.LeaveDTO GetById(int Id) { try { IDbConnection connexion = new SqlConnection(connectionString); IDbCommand commande = connexion.CreateCommand(); string query = "SELECT * FROM Leave WHERE ID=@ID"; commande.CommandText = query; commande.CommandType = CommandType.Text; commande.Parameters.Add(new SqlParameter("ID", Id)); DTO.LeaveDTO leaveDao = null; DTO.CompanyDTO companyDao = null; connexion.Open(); IDataReader dataReader = commande.ExecuteReader(); if (dataReader.Read()) { leaveDao = new DTO.LeaveDTO(); leaveDao.ID = (int)dataReader["ID"]; leaveDao.StartDate = ((DateTimeOffset)dataReader["StartDate"]).DateTime; if (dataReader["EndDate"] != DBNull.Value) { leaveDao.EndDate = ((DateTimeOffset)dataReader["EndDate"]).DateTime; } leaveDao.Description = (string)dataReader["Description"]; int companyId = (int)dataReader["Company_ID"]; companyDao = DAO.CompanyDAO.Instance.GetById(companyId); leaveDao.Company = companyDao; } return(leaveDao); } catch (Exception) { throw; } }
public void AddLeaveWithOutCompany(string description, int startDateYear, int startDateMonth, int startDateDay, int endDateYear, int endDateMonth, int endDateDay) { DTO.LeaveDTO leaveDto = new DTO.LeaveDTO(); leaveDto.StartDate = new DateTime(startDateYear, startDateMonth, startDateDay); leaveDto.EndDate = new DateTime(endDateYear, endDateMonth, endDateDay); leaveDto.Description = description; int id = 0; DTO.CompanyDTO companyDto = null; do { id++; companyDto = DAO.CompanyDAO.Instance.GetById(id); } while (companyDto != null); companyDto = new DTO.CompanyDTO(); companyDto.ID = id; leaveDto.Company = companyDto; SqlException sqlException = Assert.Throws <SqlException>(delegate() { DAO.LeaveDAO.Instance.Add(leaveDto); }); Assert.AreEqual(sqlException.Number, 547); }
public void TestLeaveDao(string name, int yearRecruitementDate, int monthRecruitementDate, int dayRecruitementDate, int?yearEndOfMissionDate, int?monthEndOfMissionDate, int?dayEndOfMissionDate, string description, int startDateYear, int startDateMonth, int startDateDay, int?endDateYear, int?endDateMonth, int?endDateDay) { // Ajouter une entreprise DTO.CompanyDTO companyDto = new DTO.CompanyDTO(); companyDto.Name = name; companyDto.RecruitementDate = new DateTime(yearRecruitementDate, monthRecruitementDate, dayRecruitementDate); if (yearEndOfMissionDate != null && monthEndOfMissionDate != null && dayEndOfMissionDate != null) { companyDto.EndOfMissionDate = new DateTime((int)yearEndOfMissionDate, (int)monthEndOfMissionDate, (int)dayEndOfMissionDate); } DAO.CompanyDAO.Instance.Add(companyDto); // Récupérer l'entreprise ajoutée IList <DTO.CompanyDTO> companies = DAO.CompanyDAO.Instance.GetAll(); int count = companies.Count; companyDto = companies[count - 1]; // Ajouter le congé DTO.LeaveDTO leaveDto = new DTO.LeaveDTO(); leaveDto.StartDate = new DateTime(startDateYear, startDateMonth, startDateDay); leaveDto.EndDate = new DateTime((int)endDateYear, (int)endDateMonth, (int)endDateDay); leaveDto.Description = description; leaveDto.Company = companyDto; DAO.LeaveDAO.Instance.Add(leaveDto); // Supprimer l'entreprise ajoutée SqlException sqlException = Assert.Throws <SqlException>(delegate() { DAO.CompanyDAO.Instance.Delete(companyDto); }); Assert.AreEqual(sqlException.Number, 547); // Récupérer le congé ajouté IList <DTO.LeaveDTO> leaves = DAO.LeaveDAO.Instance.GetAll(); count = leaves.Count; leaveDto = leaves[count - 1]; // Récupérer le congé par son ID int id = leaveDto.ID; leaveDto = DAO.LeaveDAO.Instance.GetById(id); // Modifier le congé leaveDto.Description = "XXX"; leaveDto.StartDate = DateTime.Now; leaveDto.EndDate = null; // Ajouter une entreprise DTO.CompanyDTO companyDto2 = new DTO.CompanyDTO(); companyDto2.Name = name; companyDto2.RecruitementDate = new DateTime(yearRecruitementDate, monthRecruitementDate, dayRecruitementDate); if (yearEndOfMissionDate != null && monthEndOfMissionDate != null && dayEndOfMissionDate != null) { companyDto2.EndOfMissionDate = new DateTime((int)yearEndOfMissionDate, (int)monthEndOfMissionDate, (int)dayEndOfMissionDate); } DAO.CompanyDAO.Instance.Add(companyDto2); // Récupérer l'entreprise ajoutée companies = DAO.CompanyDAO.Instance.GetAll(); count = companies.Count; companyDto2 = companies[count - 1]; leaveDto.Company = companyDto2; DAO.LeaveDAO.Instance.Update(leaveDto); // Supprimer le congé ajouté DAO.LeaveDAO.Instance.Delete(leaveDto); // Supprimer l'entreprise ajoutée DAO.CompanyDAO.Instance.Delete(companyDto); DAO.CompanyDAO.Instance.Delete(companyDto2); }
public void TestCompanyDAO(string name, int yearRecruitementDate, int monthRecruitementDate, int dayRecruitementDate, int?yearEndOfMissionDate, int?monthEndOfMissionDate, int?dayEndOfMissionDate) { // Récupérer la liste de entreprise à partir de la base de données IList <DTO.CompanyDTO> companies = DAO.CompanyDAO.Instance.GetAll(); // Mémoriser le nombre des entreprises dans la base de données int count1 = companies != null ? companies.Count : 0; // Ajouter une nouvelle entreprise DTO.CompanyDTO companyDto = new DTO.CompanyDTO(); companyDto.Name = name; companyDto.RecruitementDate = new DateTime(yearRecruitementDate, monthRecruitementDate, dayRecruitementDate); if (yearEndOfMissionDate != null && monthEndOfMissionDate != null && dayEndOfMissionDate != null) { companyDto.EndOfMissionDate = new DateTime((int)yearEndOfMissionDate, (int)monthEndOfMissionDate, (int)dayEndOfMissionDate); } DAO.CompanyDAO.Instance.Add(companyDto); // Récupérer la liste de entreprise à partir de la base de données companies = DAO.CompanyDAO.Instance.GetAll(); // Vérifié si une entreprise à été ajouté à la base de données int count2 = companies != null ? companies.Count : 0; Assert.AreEqual(count2, count1 + 1); // Récupérer la dernière entreprise ajoutée à la base de données int Id = companies[count2 - 1].ID; companyDto = DAO.CompanyDAO.Instance.GetById(Id); Assert.NotNull(companyDto); // Modifier l'entreprise récupérée companyDto.Name = "XXX"; DAO.CompanyDAO.Instance.Update(companyDto); // Récupérer une entreprise par son ID companies = DAO.CompanyDAO.Instance.Find(c => c.ID == Id); Assert.NotNull(companies); Assert.AreEqual(companies.Count, 1); companyDto = companies[0]; Assert.AreEqual(companyDto.Name, "XXX"); // Modifier l'entreprise récupérée companyDto.EndOfMissionDate = null; DAO.CompanyDAO.Instance.Update(companyDto); // Récupérer la dernière entreprise ajoutée à la base de données companyDto = DAO.CompanyDAO.Instance.GetById(Id); Assert.IsNull(companyDto.EndOfMissionDate); // Supprimer la dernière entreprise ajoutée à la base de données DAO.CompanyDAO.Instance.Delete(companyDto); // Récupérer la liste de entreprise à partir de la base de données companies = DAO.CompanyDAO.Instance.GetAll(); // Vérifié l'entreprise à été supprimée à la base de données int count3 = companies != null ? companies.Count : 0; Assert.AreEqual(count1, count3); }