/// <summary> /// Updates an item in the database /// </summary> /// <param name="item">the company that will be updated</param> /// <returns>true if it succeds</returns> public bool Update(Company item) { try { using (var ctx = new Context()) { Company result = ctx.Entry(item).Entity; if (result == null) { return false; } foreach (var empsToAttach in item.Employees) { ctx.Employees.Attach(empsToAttach); } //sets the information result.Employees = item.Employees; result.Name = item.Name; result.PhoneNr = item.PhoneNr; ctx.Entry(result).State = EntityState.Modified; ctx.SaveChanges(); return true; } } catch(Exception e) { return false; } }
public void Company_With_The_Same_Id_Should_Be_Equal_Test() { Company comp1 = new Company { Id = 1}; Company comp2 = new Company {Id =1}; Assert.AreEqual(comp1.Id,comp2.Id); }
public void Getters_And_Setters_Test() { List<Employee> employees = new List<Employee>(); Employee emp1 = new Employee() { Id = 1, FirstName = "First Employee", }; Employee emp2 = new Employee() { Id = 1, FirstName = "First Employee", }; employees.Add(emp1); employees.Add(emp2); Company comp = new Company() { Id = 1, Name = "Big Company", PhoneNr = "12345678", Employees = employees, Active = true, }; Assert.AreEqual(comp.Id, 1); Assert.AreEqual(comp.Name, "Big Company"); Assert.AreEqual(comp.PhoneNr, "12345678"); Assert.AreEqual(comp.Employees, employees); Assert.AreEqual(comp.Active,true); }
/// <summary> /// This will generate a new company from a view company /// </summary> /// <param name="comp">the company to be generated</param> /// <returns>the company that should be added to the database</returns> public Company GenerateCompany(ViewCompany comp, int _lenghtOfPassword, int _numberOfAlphabeticCharacters) { var result = new Company() {Active = true, Name = comp.Name, PhoneNr = comp.PhoneNr}; //Generates a random password, and makes a new user with the correct email var password = System.Web.Security.Membership.GeneratePassword(_lenghtOfPassword, _numberOfAlphabeticCharacters); var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new DAL.Context.Context())); var user = new ApplicationUser() { UserName = comp.Email, Email = comp.Email }; um.Create(user, password); //Generates the access string by encoding the email and the password into an array of bytes. var bytes = System.Text.Encoding.UTF8.GetBytes(comp.Email + ":" + password); result.AccessString = System.Convert.ToBase64String(bytes); result.Active = true; //This will be so that the database will make the relation between the two. var item = um.FindByEmail(comp.Email); result.IdentityId = item.Id; return result; }
//Checks wether the recieved employee is already within the company list of employees. private bool ContainsEmployee(Company comp, Employee emp) { bool contains = false; contains = comp.Employees.FirstOrDefault(c => c.Address.Equals(emp.Address) && c.BirthDate.Year == emp.BirthDate.Year && c.BirthDate.Month == emp.BirthDate.Month && c.BirthDate.Day == emp.BirthDate.Day && c.FirstName.Equals(emp.FirstName) && c.LastName.Equals(emp.LastName) && c.Phone.Equals(emp.Phone)) != null; return contains; }
/// <summary> /// Add an item, to the database /// </summary> /// <param name="item">The item to be added</param> /// <returns>The same company but now with a primary key.</returns> public Company Add(Company item) { using (var ctx = new Context()) { var result = ctx.Companies.Add(item); ctx.SaveChanges(); return result; } }
/// <summary> /// This will parse a company to a view company /// </summary> /// <param name="company">The company to be converted</param> /// <returns>A view company corresponding to the given company</returns> public ViewCompany ParseCompany(Company company) { var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new DAL.Context.Context())); var user = um.FindById(company.IdentityId); //We are working with seeded data, which we cannot give an Identity, therefore we add in this small hack. if (user == null) return new ViewCompany() { AccessString = company.AccessString, Active = company.Active, Email = "*****@*****.**", Employees = company.Employees, PhoneNr = company.PhoneNr, Id = company.Id, Name = company.Name }; else return new ViewCompany() {AccessString = company.AccessString, Active = company.Active, Email = user.Email, Employees = company.Employees, PhoneNr = company.PhoneNr, Id = company.Id, Name = company.Name}; }
public void Getters_And_Setters_Test() { Company comp = new Company() { Id = 1, Name = "Big Company", PhoneNr = "12345678", Active = true, }; List<Employee> employees = new List<Employee>(); Employee emp1 = new Employee() { Id = 1, FirstName = "First Employee", }; Employee emp2 = new Employee() { Id = 1, FirstName = "First Employee", }; employees.Add(emp1); employees.Add(emp2); Log log = new Log() { Id = 1, Company = comp, Date = DateTime.Now.Date, Import = true, Employees = employees, Active=true }; Assert.AreEqual(log.Id, 1); Assert.AreEqual(log.Company, comp); Assert.AreEqual(log.Date, DateTime.Now.Date); Assert.AreEqual(log.Import, true); Assert.AreEqual(log.Employees, employees); Assert.AreEqual(log.Active, true); }
public void Getters_And_Setters_Test() { Company comp = new Company() { Id = 1, Name = "Big Company", PhoneNr = "12345678", Active = true, }; List<Log> logs = new List<Log>(); Log log = new Log() { Id = 1, Company = comp, Date = DateTime.Now.Date, Import = true, }; Log log2 = new Log() { Id = 1, Company = comp, Date = DateTime.Now.Date, Import = false, }; logs.Add(log); logs.Add(log2); Employee emp = new Employee() { Id = 1, FirstName = "First ", LastName = "Employee", Country = "Denmark", ZipCode = 6700, City = "Esbjerg", Address = "This Vej 7", BirthDate = DateTime.Now.Date, Rank = "Captain", Phone = "1111111", Company = comp, Active =true, Logs = logs }; Assert.AreEqual(emp.Id, 1); Assert.AreEqual(emp.FirstName, "First "); Assert.AreEqual(emp.LastName, "Employee"); Assert.AreEqual(emp.Country, "Denmark"); Assert.AreEqual(emp.Address, "This Vej 7"); Assert.AreEqual(emp.ZipCode, 6700); Assert.AreEqual(emp.City, "Esbjerg"); Assert.AreEqual(emp.BirthDate, DateTime.Now.Date); Assert.AreEqual(emp.Rank, "Captain"); Assert.AreEqual(emp.Phone,"1111111"); Assert.AreEqual(emp.Company, comp); Assert.IsTrue(emp.Active); Assert.AreEqual(emp.Logs, logs); }
/// <summary> /// will deactivate an item /// </summary> /// <param name="item">the item to be deactivated</param> /// <returns>true if the item was successfully deactivated</returns> public bool ChangeState(Company item) { using (var ctx = new Context()) { var result = ctx.Entry(item).Entity; if (result == null) { return false; } if (result.Active) { result.Active = false; } else { result.Active = true; } ctx.Entry(result).State = EntityState.Modified; ctx.SaveChanges(); return true; } }
public void AppendCompanyInfo(Company company) { string sql = "update Company set CorporateRepresentative=N'" + company.CorporateRepresentive + "', RepPhone='" + company.RepPhone + "', Contact=N'" + company.Contact + "', ContactPhone='" + company.ContactPhone + "',ContactAddress='" + company.ContactAddress + "',ConstructionContent=N'" + company.ConstructionContent + "',Note=N'" + company.Note + "' where Id=" + company.Id; DBHelper.ExecuteNonQuery(sql); }
public void UpdateCompanyPics(Company company) { string sql = @"declare @refpath nvarchar(400); declare @blpath nvarchar(400); declare @scpath nvarchar(400); declare @ripath nvarchar(400); declare @cipath nvarchar(400);"; if (company.ReferreIDPic == "") { sql += " set @refpath=''; "; } else { sql += " select @refpath = FilePath from BidDocument where FilePath like '%" + company.ReferreIDPic + "%'; "; } if (company.BusinessLicensePic == "") { sql += " set @blpath=''; "; } else { sql += " select @blpath = FilePath from BidDocument where FilePath like '%" + company.BusinessLicensePic + "%'; "; } if (company.SecurityCertificatePic == "") { sql += " set @scpath=''; "; } else { sql += " select @scpath = FilePath from BidDocument where FilePath like '%" + company.SecurityCertificatePic + "%'; "; } if (company.RepIDPic == "") { sql += " set @ripath=''; "; } else { sql += " select @ripath = FilePath from BidDocument where FilePath like '%" + company.RepIDPic + "%'; "; } if (company.ContactIDPic == "") { sql += " set @cipath=''; "; } else { sql += " select @cipath = FilePath from BidDocument where FilePath like '%" + company.ContactIDPic + "%'; "; } sql += "update company set BusinessLicensePic=@blpath,ContactIDPic=@cipath,ReferreIDPic=@refpath, RepIDPic=@ripath, SecurityCertificatePic=@scpath where id=" + company.Id + "; "; if (company.ReferreIDPic != "") { sql += "update BidDocument set ProjId=" + company.Id + " where FilePath like '%" + company.ReferreIDPic + "%'; "; } if (company.BusinessLicensePic != "") { sql += "update BidDocument set ProjId=" + company.Id + " where FilePath like '%" + company.BusinessLicensePic + "%'; "; } if (company.SecurityCertificatePic != "") { sql += "update BidDocument set ProjId=" + company.Id + " where FilePath like '%" + company.SecurityCertificatePic + "%'; "; } if (company.RepIDPic != "") { sql += "update BidDocument set ProjId=" + company.Id + " where FilePath like '%" + company.RepIDPic + "%'; "; } if (company.ContactIDPic != "") { sql += "update BidDocument set ProjId=" + company.Id + " where FilePath like '%" + company.ContactIDPic + "%'; "; } DBHelper.ExecuteNonQuery(sql); }
public string CreateCompany(Company company, string uid) { string sql = ""; sql += @"insert into Company(Name, CreditNo, RegisteredCapital, BusinessType, BusinessScope, QualificationLevel, SecurityCertificateNo, CorporateRepresentative, RepPhone, Contact, ContactPhone, ContactAddress, ConstructionContent, Note, Status, Type, Referre,AuditDate, AuditStatus, SubmitUserId, CreateDate, OutCompanyProjId, AddYear)" + "values(N'" + company.Name + "', N'" + company.CreditNo + "', " + company.RegisteredCapital + ", " + "N'" + company.BusinessType + "', N'" + company.BusinessScope + "', N'" + company.QualificationLevel + "', " + "'" + company.SecurityCertificateNo + "', N'" + company.CorporateRepresentive + "', '" + company.RepPhone + "', " + "N'" + company.Contact + "', '" + company.ContactPhone + "', N'" + company.ContactAddress + "', " + "N'" + company.ConstructionContent + "', N'" + company.Note + "', " + company.Status + ", " + company.Type + ", N'" + company.Referrer + "', getdate(), " + company.AuditStatus + ", " + uid + @", getdate()," + company.OutCompanyProjId + ", " + company.AddYear + ");"; sql += "select max(id) from Company;"; string i = DBHelper.ExecuteScalar(CommandType.Text, sql); sql = @"declare @did int select @did=ID from Department where ProjectDp=1 and ID in ( select PID from Department where ID in (select du.departmentid from DepartmentUser du where du.UserId=" + uid + @")) update Company set DepartmentID = @did where ID=" + i; DBHelper.ExecuteNonQuery(sql); if (company.AuditStatus == 1) { DataTable dt = null; if (company.Type == 1) { // 名录内企业 dt = CreateApproveProcess(i, uid, "5"); } else { // 名录外企业 dt = CreateApproveProcess(i, uid, "1"); } string puser = DBHelper.ExecuteScalar("select UserName from userinfo where id=" + uid); try { string reurl = ""; string aptype = ""; switch (company.Type == 0 ? "1" : "5") { case "1": aptype = "名录外企业审批"; reurl = "http://cr16gd.saibo.net.cn/MobileCompany?id=" + i + "&approved=1&userid="; break; case "5": aptype = "名录内企业审批"; reurl = "http://cr16gd.saibo.net.cn/MobileCompany?id=" + i + "&approved=1&userid="; break; } WXMessage.WXMessage message = new WXMessage.WXMessage(); for (int j = 0; j < dt.Rows.Count; j++) { uid = dt.Rows[j][1].ToString(); string guid = Guid.NewGuid().ToString().Replace("-", ""); reurl += uid + "&lcode=" + guid; message.SendTempletMessge(dt.Rows[j][0].ToString(), reurl, aptype, company.Name, puser); DBHelper.ExecuteNonQuery("insert into WXLoginSession(code, dtime, userid) values('" + guid + "',getdate()," + uid + ", 0)"); } } catch (Exception e) { Log ll = new Log(); ll.OperType = OperateType.SendWXInfo; ll.UserId = uid; ll.Description = "发送短信失败:" + e.ToString(); LogContext.WriteLog(ll); } Log l = new Log(); l.OperType = OperateType.Create; l.UserId = uid; l.Description = "创建公司" + company.Type == "1"?"名录内":"名录外" + " - " + company.Name; LogContext.WriteLog(l); } return(i); }
public void Company_With_Different_Id_Should_Be_Unequal_And_Have_Different_HashCode_Test() { Company comp1 = new Company { Id = 1 }; Company comp2 = new Company { Id = 2 }; Assert.AreNotEqual(comp1,comp2); }