Beispiel #1
0
 public User(Email email, UserName name, string password, UserGroup userGroup)
 {
     this.Email = email;
     this.Name = name;
     this.Password = password;
     UserGroup = userGroup;
 }
        public void DeleteEmail(Email email)
        {
            Contract.Requires<ArgumentNullException>(email != null, "The email must be non-null!");

            using (var context = new SchoolContext())
            {
                context.Emails.Attach(email);
                context.Emails.Remove(email);

                if (context.SaveChanges() != 0)
                {
                    /*
                    if (!Logger.IsLoggingEnabled())
                    {
                        Logger.Write("The email " + email + " was successfully removed!", "General", 2, 2,
                            TraceEventType.Information);
                    }*/
                }/*
                {
                    if (!Logger.IsLoggingEnabled())
                    {
                        Logger.Write("The email " + email + " was not removed!", "Important", 1, 1, TraceEventType.Error);
                    }
                }*/
            }
        }
        /*
        [NotNullValidator(MessageTemplate = "The student of the email cannot be null")]
        public virtual Student Student { get; set; }

        [RangeValidator(1, RangeBoundaryType.Exclusive, 999, RangeBoundaryType.Ignore, MessageTemplate = "The student id must be > 0")]
        public virtual int StudentId { get; set; }
        */
        public void ValidateStudentIsPresent(Email email, ValidationResults results)
        {
            if (email.StudentId == 0 && email.Student == null)
            {
                results.AddResult(new ValidationResult
                    (
                    "The email must have a student", results, "ValidationMethod", "error", null)
                    );
            }
        }
 // Create
 public void AddEmail(Email email)
 {
     if (!Validate(email))
     {
         var sb = new StringBuilder();
         foreach (var error in Errors)
         {
             sb.Append(error);
         }
         throw new ValidationException(sb.ToString());
     }
     DataMapperFactory.GetMapperFactory().EmailMapper.AddEmail(email);
 }
        public void DeleteEmail(int emailId)
        {
            Contract.Requires<ArgumentOutOfRangeException>(emailId > 0, "The emailId must be > 0!");

            using (var context = new SchoolContext())
            {
                var emailToBeDeleted = new Email()
                {
                   EmailId = emailId
                };

                try
                {
                    context.Emails.Attach(emailToBeDeleted);
                    context.Emails.Remove(emailToBeDeleted);

                    if (context.SaveChanges() != 0)
                    {
                        /*
                        if (!Logger.IsLoggingEnabled())
                        {
                            Logger.Write("The email " + emailToBeDeleted + " was successfully removed!", "General", 2, 2,
                                TraceEventType.Information);
                        }*/
                    }/*
                    else
                    {
                        if (!Logger.IsLoggingEnabled())
                        {
                            Logger.Write("The email " + emailToBeDeleted + " was not removed!", "Important", 1, 1,
                                TraceEventType.Error);
                        }
                    }*/
                }
                catch
                {
                    throw new NoSuchEntityException("The semester with the id=" + emailToBeDeleted + " does not exist in the database");
                }
            }
        }
        public void AddEmail(Email email)
        {
            Contract.Requires<ArgumentNullException>(email != null, "The email cannot be null");

            using (var context = new SchoolContext())
            {
                context.Emails.Add(email);

                if (context.SaveChanges() != 0)
                {/*
                    if (Logger.IsLoggingEnabled())
                    {
                        Logger.Write("The email: " + email + " has been successfully added!", "General", 2, 2, TraceEventType.Information);
                    }*/
                }/*
                else
                {
                    if (Logger.IsLoggingEnabled())
                    {
                        Logger.Write("The email: " + email + " insertion has failed!", "Important", 1, 1, TraceEventType.Error);
                    }
                }*/
            }
        }
 // Update
 public Email UpdateEmail(int studentId, Email student)
 {
     return DataMapperFactory.GetMapperFactory().EmailMapper.UpdateEmail(studentId, student);
 }
 public void DeleteEmail(Email student)
 {
     DataMapperFactory.GetMapperFactory().EmailMapper.DeleteEmail(student);
 }
        public void TestUpdateEmailWithObjectNull()
        {
            //create entity instance
            var student = new Student
            {
                FirstName = "xqwe",
                LastName = "yrtret",
                CNP = "1234567891234",
                SID = 123,
                EnrollmentDate = DateTime.Now,
                Phones = new[] { new Phone("0123 123456"), }
            };

            var email = new Email("*****@*****.**")
            {
                Student = student
            };

            var emailUp = new Email("*****@*****.**")
            {
                EmailAddress = "*****@*****.**"
            };

            //insert entity
            var res = Validation.Validate(student);
            Assert.AreEqual(res.IsValid, true);
            _emailService.AddEmail(email);

            //change entity properties;update entity
            int lastEmailId = email.EmailId;
            var updatedStudent1 = _emailService.UpdateEmail(lastEmailId, (Email) null);

            //read updated entity
            var updateStudent2 = _emailService.GetEmailById(lastEmailId);

            //compare changed entity and read entity
            Assert.AreEqual("*****@*****.**", updateStudent2.EmailAddress);

            //delete entity
            _emailService.DeleteEmail(email);
        }
        public void TestAddEmailFailure()
        {
            var student = new Student
            {
                FirstName = "xqwe",
                LastName = "yrtret",
                CNP = "1234567891234",
                SID = 123,
                EnrollmentDate = DateTime.Now,
                Phones = new[] { new Phone("0123 123456"), }
            };

            var email = new Email("aaatest.com")
            {
                Student = student
            };

            _emailService.AddEmail(email);

            var emailId = email.EmailId;

            Assert.AreEqual(_emailService.IsEmailInserted(emailId), true);

            _emailService.DeleteEmail(emailId);
        }
        public void TestDeleteEmailWithEmailObject()
        {
            var student = new Student
            {
                FirstName = "xqwe",
                LastName = "yrtret",
                CNP = "1234567891234",
                SID = 123,
                EnrollmentDate = DateTime.Now
            };

            var email = new Email("*****@*****.**")
            {
                Student = student
            };

            var res = Validation.Validate(email);
            Assert.AreEqual(res.IsValid, true, res.IsValid ? "" : "Validation Failed: " + res.Single().Message);

            if (!res.IsValid)
            {
                var singleOrDefault = res.SingleOrDefault();
                if (singleOrDefault != null) Trace.WriteLine(singleOrDefault.Message);
            }

            _emailService.AddEmail(email);

            var emailId = email.EmailId;

            _emailService.DeleteEmail(email);

            Assert.IsNull(_emailService.GetEmailById(emailId));
        }
 public void TestUpdateEmailEmail2()
 {
     var email = new Email()
     {
         EmailId = 22
     };
     new EmailService().UpdateEmail(email.EmailId, email.EmailAddress);
 }
 public void TestUpdateEmailEmail1()
 {
     Email email = new Email();
     new EmailService().UpdateEmail(0, email);
 }
        public void TestIsEmailInsertedSuccess()
        {
            var student = new Student
            {
                FirstName = "xqwe",
                LastName = "yrtret",
                CNP = "1234567891234",
                SID = 123,
                EnrollmentDate = DateTime.Now,
                Phones = new[] { new Phone("0123 123456"), }
            };

            var email = new Email("*****@*****.**")
            {
                Student = student
            };

            var res = Validation.Validate(email);
            Assert.AreEqual(res.IsValid, true, res.IsValid ? "" : "Validation Failed: " + res.Single().Message);

            if (!res.IsValid)
            {
                var singleOrDefault = res.SingleOrDefault();
                if (singleOrDefault != null) Trace.WriteLine(singleOrDefault.Message);
            }

            _emailService.AddEmail(email);

            var emailId = email.EmailId;

            Assert.AreEqual(_emailService.IsEmailInserted(emailId), true);

            _emailService.DeleteEmail(emailId);
        }
        public void TestGetAllEmails2()
        {
            //Arrange
            var student = new Student
            {
                FirstName = "firstname1",
                LastName = "lastname1",
                CNP = "1234567891234",
                SID = 123,
                EnrollmentDate = DateTime.Now,
                Emails = new[] { new Email("*****@*****.**"), }
            };
            var address = new Address
            {
                City = "New York",
                Country = "USA",
                State = "NYQ",
                PostalCode = "12356",
                Street = "353",
                Student = student
            };

            student.Address = address;

            using (var st = new StudentService())
            {
                st.AddStudent(student);
            }

            var email = new Email("*****@*****.**") { Student = student };
            var res = Validation.Validate(email);
            Assert.AreEqual(res.IsValid, true, res.IsValid ? "" : "Validation Failed: " + res.First().Message);

            if (!res.IsValid)
            {
                var firstOrDefault = res.FirstOrDefault();
                if (firstOrDefault != null) Trace.WriteLine(firstOrDefault.Message);
            }

               // using (var es = new EmailService())
               // {
            //    var lastCount = es.GetAllEmails().Count;
            //    es.AddEmail(email);

                //Assert.AreEqual(lastCount + 1, es.GetAllEmails().Count);
              //  }
        }
        public void TestEmailStudentIsNull()
        {
            var student = new Student
            {
                FirstName = "firstname1",
                LastName = "lastname1",
                CNP = "1234567891234",
                SID = 123,
                EnrollmentDate = DateTime.Now,
            };

            using (var es = new EmailService())
            {
                var email = new Email("*****@*****.**") { Student = null };
                var res = Validation.Validate(email);
                Assert.AreEqual(res.IsValid, false, res.IsValid ? "" : "Validation Failed: " + res.First().Message);

                if (!res.IsValid)
                {
                    var firstOrDefault = res.FirstOrDefault();
                    if (firstOrDefault != null) Trace.WriteLine(firstOrDefault.Message);
                }
            }
        }
        public void TestEmailIsIsOfProperMaximumLength()
        {
            var student = new Student
            {
                FirstName = "firstname1",
                LastName = "lastname1",
                CNP = "1234567891234",
                SID = 123,
                EnrollmentDate = DateTime.Now,
            };

            using (var es = new EmailService())
            {
                var email = new Email("*****@*****.**") { Student = student };
                var res = Validation.Validate(email);
                Assert.AreEqual(res.IsValid, true, res.IsValid ? "" : "Validation Failed: " + res.First().Message);

                if (!res.IsValid)
                {
                    var firstOrDefault = res.FirstOrDefault();
                    if (firstOrDefault != null) Trace.WriteLine(firstOrDefault.Message);
                }
            }
        }
 private bool Validate(Email email)
 {
     var res = Validation.Validate(email);
     if (res.IsValid) return true;
     res.ForEach(r => Errors.Add(r.Message));
     return false;
 }
        public Email UpdateEmail(int emailId, Email email)
        {
            Contract.Requires<ArgumentOutOfRangeException>(emailId > 0, "The emailId must be > 0!");
            Contract.Requires<ArgumentNullException>(email != null, "The email must be non-null!");

            using (var context = new SchoolContext())
            {
                var emailToBeUpdated = context.Emails.SingleOrDefault(e => e.EmailId == emailId);

                if (emailToBeUpdated != null)
                {
                    emailToBeUpdated.EmailAddress = email.EmailAddress;

                    if (context.SaveChanges() != 0)
                    {
                        /*
                        if (Logger.IsLoggingEnabled())
                        {
                            Logger.Write("The email " + emailToBeUpdated + " was successfully updated!", "General", 2, 2,
                                TraceEventType.Information);
                        }*/
                    }
                        /*
                    else
                    {
                        if (!Logger.IsLoggingEnabled())
                        {
                            Logger.Write("The email " + emailToBeUpdated + " was not updated!", "Important", 1, 1,
                                TraceEventType.Error);
                        }
                    }*/

                    return emailToBeUpdated;
                }
                else
                {
                    /*
                    if (Logger.IsLoggingEnabled())
                    {
                        Logger.Write("The email with id: " + emailId + " doesn't exist in the database!", "Important", 1,
                            1, TraceEventType.Error);
                    }*/

                    throw new NoSuchEntityException("The email with id: " + emailId +
                                                    " doesn't exist in the database!");
                }
            }
        }
        public void TestAddEmailToStudent()
        {
            var student = new Student
            {
                FirstName = "firstname1",
                LastName = "lastname1",
                CNP = "1234567891234",
                SID = 123,
                EnrollmentDate = DateTime.Now,
                Emails = new[] { new Email("*****@*****.**") }
            };

            var address = new Address
            {
                City = "New York",
                Country = "USA",
                State = "NYQ",
                PostalCode = "12356",
                Street = "353",
                Student = student
            };

            student.Address = address;

            using (var es = new EmailService())
            using (var st = new StudentService())
            {
                st.AddStudent(student);
                var email = new Email("*****@*****.**") { StudentId = student.StudentId };
                es.AddEmail(email);
                var res = Validation.Validate(email);
                Assert.AreEqual(res.IsValid, true, res.IsValid ? "" : "Validation Failed: " + res.First().Message);

                if (!res.IsValid)
                {
                    var firstOrDefault = res.FirstOrDefault();
                    if (firstOrDefault != null) Trace.WriteLine(firstOrDefault.Message);
                }

                var stud = st.GetStudentById(student.StudentId);
                Assert.AreEqual(stud.StudentId, student.StudentId);
                Assert.IsTrue(stud.Emails.Any(e => e.EmailAddress == email.EmailAddress));
            }
        }
 public void TestUpdateEmailEmail3()
 {
     Email email = new Email()
     {
         EmailId = 11,
         EmailAddress = "*****@*****.**",
         StudentId = 22
     };
     new EmailService().UpdateEmail(email.StudentId, student: null);
 }
        public void TestAddEmailWithStudentIdZero()
        {
            var email = new Email("*****@*****.**") { StudentId = 0 };

            var res = Validation.Validate(email);
            Assert.AreEqual(res.IsValid, false, res.IsValid ? "" : "Validation Failed: " + res.First().Message);

            if (!res.IsValid)
            {
                var firstOrDefault = res.FirstOrDefault();
                if (firstOrDefault != null) Trace.WriteLine(firstOrDefault.Message);
            }
        }
Beispiel #23
0
 public bool Equals(Email other)
 {
     if (ReferenceEquals(null, other)) return false;
     if (ReferenceEquals(this, other)) return true;
     return Equals(other.EmailAddress, EmailAddress);
 }
 public void TestUpdateEmailEmailNotExisting()
 {
     Email email = new Email();
     new EmailService().UpdateEmail(9999, email);
 }