public async Task <IOperationResult <bool> > Update(PermitCreateOrEditViewModel permitToUpdate) { try { IOperationResult <string> validationResult = ValidatePermit(permitToUpdate); if (!validationResult.Success) { return(OperationResult <bool> .Fail(validationResult.Message)); } Permit permit = await _permitRepositoy.FindAsync(permit => permit.Id == permitToUpdate.Id); PermitType permitType = await _permitTypeRepository.FindAsync(permitType => permitType.Id == permitToUpdate.PermitType); if (permitType == null) { return(OperationResult <bool> .Fail("No se encontro el permiso en la base de datos.")); } permit.EmployeeName = permitToUpdate.EmployeeName; permit.EmployeeLastName = permitToUpdate.EmployeeLastName; permit.PermitType = permitType; permit.Date = permitToUpdate.Date; await _permitRepositoy.SaveAsync(); return(OperationResult <bool> .Ok(true)); } catch { return(OperationResult <bool> .Fail("Ha ocurrido un error al actualizar el permiso.")); } }
public async Task <IOperationResult <bool> > Create(PermitCreateOrEditViewModel permit) { try { IOperationResult <string> validationResult = ValidatePermit(permit); if (!validationResult.Success) { return(OperationResult <bool> .Fail(validationResult.Message)); } IOperationResult <Permit> permitToCreateResult = await BuildPermit(permit); if (!permitToCreateResult.Success) { return(OperationResult <bool> .Fail(permitToCreateResult.Message)); } _permitRepositoy.Create(permitToCreateResult.Entity); await _permitRepositoy.SaveAsync(); return(OperationResult <bool> .Ok(true)); } catch { return(OperationResult <bool> .Fail("Ha ocurrido un error al registrar el permiso.")); } }
public async Task <IActionResult> Update(PermitCreateOrEditViewModel permit) { IOperationResult <bool> operationResult = await _permitManager.Update(permit); if (!operationResult.Success) { return(BadRequest(operationResult.Message)); } return(Ok()); }
private IOperationResult <string> ValidatePermit(PermitCreateOrEditViewModel permit) { if (string.IsNullOrWhiteSpace(permit.EmployeeName)) { return(OperationResult <string> .Fail("El nombre del empleado no puede estar vacio.")); } if (string.IsNullOrWhiteSpace(permit.EmployeeLastName)) { return(OperationResult <string> .Fail("El apellido del empleado no puede estar vacio.")); } if (permit.PermitType == default(int)) { return(OperationResult <string> .Fail("El tipo de permiso no es valido.")); } return(OperationResult <string> .Ok()); }
public async Task CreateWhenEmployeePermitTypeIs0ThenReturnOperationResultFailed() { PermitCreateOrEditViewModel permitCreateViewModel = new PermitCreateOrEditViewModel { EmployeeName = "sadsad", EmployeeLastName = "sadsad", PermitType = 0, Date = DateTime.Now }; IPermitRepositoy permitRepository = Substitute.For <IPermitRepositoy>(); IPermitTypeRepository permitTypeRepository = Substitute.For <IPermitTypeRepository>(); PermitManager permitManager = new PermitManager(permitRepository, permitTypeRepository); IOperationResult <bool> operationResult = await permitManager.Create(permitCreateViewModel); Assert.IsFalse(operationResult.Success); Assert.AreEqual("El tipo de permiso no es valido.", operationResult.Message); }
public async Task CreateWhenEmployeeNameIsEmptyThenReturnOperationResultFailed() { PermitCreateOrEditViewModel permitCreateViewModel = new PermitCreateOrEditViewModel { EmployeeName = "", EmployeeLastName = "alalal", PermitType = 0, Date = DateTime.Now }; IPermitRepositoy permitRepository = Substitute.For <IPermitRepositoy>(); IPermitTypeRepository permitTypeRepository = Substitute.For <IPermitTypeRepository>(); PermitManager permitManager = new PermitManager(permitRepository, permitTypeRepository); IOperationResult <bool> operationResult = await permitManager.Create(permitCreateViewModel); Assert.IsFalse(operationResult.Success); Assert.AreEqual("El nombre del empleado no puede estar vacio.", operationResult.Message); }
private async Task <IOperationResult <Permit> > BuildPermit(PermitCreateOrEditViewModel permitToCreate) { PermitType permitType = await _permitTypeRepository.FindAsync(permitType => permitType.Id == permitToCreate.PermitType); if (permitType == null) { return(OperationResult <Permit> .Fail("No se encontro el permiso en la base de datos.")); } var permit = new Permit { Id = 0, EmployeeName = permitToCreate.EmployeeName, EmployeeLastName = permitToCreate.EmployeeLastName, PermitType = permitType, Date = permitToCreate.Date }; return(OperationResult <Permit> .Ok(permit)); }
public async Task CreateWhenPermitIsCorrecThenReturnOperationResultSuccess() { PermitCreateOrEditViewModel permitCreateViewModel = new PermitCreateOrEditViewModel { EmployeeName = "sadsad", EmployeeLastName = "sadsad", PermitType = 1, Date = DateTime.Now }; IPermitRepositoy permitRepository = Substitute.For <IPermitRepositoy>(); IPermitTypeRepository permitTypeRepository = Substitute.For <IPermitTypeRepository>(); permitTypeRepository.FindAsync(Arg.Any <Expression <Func <PermitType, bool> > >()).ReturnsForAnyArgs(new PermitType()); PermitManager permitManager = new PermitManager(permitRepository, permitTypeRepository); IOperationResult <bool> operationResult = await permitManager.Create(permitCreateViewModel); Assert.IsTrue(operationResult.Success); }
public async Task UpdateWhenPermitTypeIsNullThenReturnOperationResultFailed() { PermitCreateOrEditViewModel permitCreateViewModel = new PermitCreateOrEditViewModel { EmployeeName = "sadsad", EmployeeLastName = "sadsad", PermitType = 1, Date = DateTime.Now }; IPermitRepositoy permitRepository = Substitute.For <IPermitRepositoy>(); IPermitTypeRepository permitTypeRepository = Substitute.For <IPermitTypeRepository>(); permitTypeRepository.FindAsync(Arg.Any <Expression <Func <PermitType, bool> > >()).ReturnsForAnyArgs(default(PermitType)); PermitManager permitManager = new PermitManager(permitRepository, permitTypeRepository); IOperationResult <bool> operationResult = await permitManager.Update(permitCreateViewModel); Assert.IsFalse(operationResult.Success); Assert.AreEqual("No se encontro el permiso en la base de datos.", operationResult.Message); }
public async Task CreateWhenExceptionOcuredThenReturnOperationResultFailed() { PermitCreateOrEditViewModel permitCreateViewModel = new PermitCreateOrEditViewModel { EmployeeName = "sadasd", EmployeeLastName = "alalal", PermitType = 51, Date = DateTime.Now }; IPermitRepositoy permitRepository = Substitute.For <IPermitRepositoy>(); IPermitTypeRepository permitTypeRepository = Substitute.For <IPermitTypeRepository>(); permitRepository.When(p => p.Create(Arg.Any <Permit>())).Do(call => { throw new ArgumentException(); }); permitTypeRepository.FindAsync(Arg.Any <Expression <Func <PermitType, bool> > >()).ReturnsForAnyArgs(new PermitType()); PermitManager permitManager = new PermitManager(permitRepository, permitTypeRepository); IOperationResult <bool> operationResult = await permitManager.Create(permitCreateViewModel); Assert.IsFalse(operationResult.Success); Assert.AreEqual("Ha ocurrido un error al registrar el permiso.", operationResult.Message); }