Exemple #1
0
        public override bool Equals(object obj)
        {
            bool equal = false;

            if (obj != null && obj.GetType().Equals(this.GetType()))
            {
                Colaborator colaborator = (Colaborator)obj;
                if (this.mail.Equals(colaborator.mail))
                {
                    equal = true;
                }
            }
            return(equal);
        }
        public void UpdateColaborator(Colaborator colaborator)
        {
            if (colaborator == null || colaborator.Id <= 0)
                throw new ArgumentException("colaborator");

            var validator = DataAnnotationsEntityValidatorFactory.Create();
            if (!validator.IsValid(colaborator))
                throw new ApplicationValidationErrorsException(validator.GetValidationMessages(colaborator));

            if (_colaboratorRepository.ExistsAnother(colaborator.Registry, colaborator.Id))
                throw new ApplicationValidationErrorsException(new Error { Key = "Registry", Value = "Já existe um colaborador com essa matricula" });

            _colaboratorRepository.Modify(colaborator);
            _colaboratorRepository.UnityOfWork.Commit();
        }
        public HttpResponseMessage Put(Colaborator colaborator)
        {
            if (colaborator == null || colaborator.Id == 0)
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Este colaborador não existe");

            var service = ColaboratorServiceFactory.Create();

            try
            {
                service.UpdateColaborator(colaborator);
            }
            catch (ApplicationValidationErrorsException exception)
            {
                var errors = new ModelStateDictionary();
                foreach (var validationError in exception.ValidationErrors)
                    errors.AddModelError(validationError.Key, validationError.Value);
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, errors);
            }

            return Request.CreateResponse(HttpStatusCode.OK, colaborator);
        }
        public HttpResponseMessage Post(Colaborator colaborator)
        {
            if (colaborator == null)
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Colaborador Inválido");

            var service = ColaboratorServiceFactory.Create();

            try
            {
                service.AddColaborator(colaborator);
            }
            catch (ApplicationValidationErrorsException exception)
            {
                var errors = new ModelStateDictionary();
                foreach (var error in exception.ValidationErrors)
                    errors.AddModelError(error.Key, error.Value);
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, errors);
            }

            return Request.CreateResponse(HttpStatusCode.OK, colaborator);
        }
 private void FakeUpdateColaborator(Colaborator colaborator)
 {
     foreach (var colaboratorPersisted in _colaborators.Where(colaborator1 => colaborator1.Id == colaborator.Id))
     {
         colaboratorPersisted.Name = colaborator.Name;
         colaboratorPersisted.Registry = colaborator.Registry;
         colaboratorPersisted.DateOfBirth = colaborator.DateOfBirth;
         colaboratorPersisted.PhoneNumber = colaborator.PhoneNumber;
         colaboratorPersisted.Address = colaborator.Address;
         colaboratorPersisted.Estate = colaborator.Estate;
         colaboratorPersisted.City = colaborator.City;
     }
 }
 private void FakeAddColaborator(Colaborator colaborator)
 {
     var id = _colaborators.Max(x => x.Id) + 1;
     colaborator.Id = id;
     _colaborators.Add(colaborator);
 }
        public void ShouldBeUpdateAColaborator()
        {
            var toUpdate = new Colaborator
                               {
                                   Id = 1,
                                   Address = "Rua Juriti, 1849",
                                   City = "Ariquemes",
                                   DateOfBirth = new DateTime(1990, 06, 02),
                                   Estate = "BA",
                                   Name = "Pedro Silva",
                                   PhoneNumber = "(69) 9949-0353",
                                   Registry = "aabbcc1222"
                               };

            var errors = new List<Error>();

            try
            {
                _colaboratorService.UpdateColaborator(toUpdate);
            }
            catch (ApplicationValidationErrorsException exception)
            {
                errors.AddRange(exception.ValidationErrors);
            }

            var expectedColaborator = _colaborators.FirstOrDefault(x => x.Id == 1);

            Assert.IsFalse(errors.Any());
            Assert.IsNotNull(expectedColaborator);
            Assert.AreEqual(toUpdate.Registry, expectedColaborator.Registry);
        }
        public void ShouldBeTryAddAColaboratorWithAEmptyRegistry()
        {
            var colaborator = new Colaborator
            {
                Name = "Colaborador Invalido",
                DateOfBirth = new DateTime(2000, 11, 02),
                PhoneNumber = "(69) 9949-0353",
                Address = "Rua Perdizes",
                Estate = "RO",
                City = "Ariquemes"
            };

            var errors = new List<Error>();

            try
            {
                _colaboratorService.AddColaborator(colaborator);
            }
            catch (ApplicationValidationErrorsException exception)
            {
                errors.AddRange(exception.ValidationErrors);
            }
            Assert.AreEqual("Registry", errors.FirstOrDefault().Key);
        }
        public void ShouldBeAddAColaboratorSuccess()
        {
            var colaborator = new Colaborator
                                              {
                                                  Address = "Rua dos Testes",
                                                  City = "Ariquemes",
                                                  DateOfBirth = new DateTime(2012, 06, 02),
                                                  Estate = "RO",
                                                  Registry = "aabbcc1232",
                                                  Name = "Jose Pereira",
                                                  PhoneNumber = "(99) 9999-9999"
                                              };

            var errors = new List<Error>();

            try
            {
                _colaboratorService.AddColaborator(colaborator);
            }
            catch (ApplicationValidationErrorsException exception)
            {
                errors.AddRange(exception.ValidationErrors);
            }

            Assert.IsFalse(errors.Any());
            Assert.AreEqual(3, colaborator.Id);
        }
        public void ColaboratorRepositoryShouldBeAddColaboratorAndSave()
        {
            var colaborator = new Colaborator
            {
                Name = "Fake Colaborator New",
                DateOfBirth = new DateTime(2000, 01, 01),
                Registry = "66",
                PhoneNumber = "(69) 9944-4444",
                Address = "Fake Street, 66",
                Estate = "FE",
                City = "Fake City"
            };

            _repository.Add(colaborator);
            _repository.UnityOfWork.Commit();

            Assert.IsTrue(colaborator.Id > 0);
        }