public IHttpActionResult PostEmployeeDrivingLicense(EmployeeDrivingLicenseDTO employeeDrivingLicense) { if (employeeDrivingLicense == null) { return(BadRequest(ModelState)); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { EmployeeDrivingLicense license = employeeDrivingLicense.FromDTO(); UnitOfWork unitOfWork = new UnitOfWork(factory); license.Id = license.NewId(unitOfWork); unitOfWork.EmployeeDrivingLicensesRepository.Insert(license); unitOfWork.Save(); EmployeeDrivingLicenseDTO dto = license.ToDTO(); return(CreatedAtRoute("GetDrivingLicense", new { id = dto.Id }, dto)); } catch (NotFoundException nfe) { return(NotFound()); } catch (ConflictException ce) { return(Conflict()); } catch (Exception e) { return(BadRequest(e.Message)); } }
public IHttpActionResult DeleteEmployeeDrivingLicense(int id) { try { UnitOfWork unitOfWork = new UnitOfWork(factory); EmployeeDrivingLicense license = unitOfWork.EmployeeDrivingLicensesRepository.GetByID(id); license.Deleted = true; unitOfWork.EmployeeDrivingLicensesRepository.Update(license); unitOfWork.Save(); EmployeeDrivingLicenseDTO dto = license.ToDTO(); return(Ok(dto)); } catch (NullReferenceException nle) { return(NotFound()); } catch (NotFoundException nfe) { return(NotFound()); } catch (ConflictException ce) { return(Conflict()); } catch (Exception e) { return(BadRequest(e.Message)); } }
public IHttpActionResult PutEmployeeDrivingLicense(int id, EmployeeDrivingLicenseDTO employeeDrivingLicense) { if (employeeDrivingLicense == null || !ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != employeeDrivingLicense.Id) { return(BadRequest()); } try { EmployeeDrivingLicense license = employeeDrivingLicense.FromDTO(); UnitOfWork unitOfWork = new UnitOfWork(factory); unitOfWork.EmployeeDrivingLicensesRepository.Update(license); unitOfWork.Save(); EmployeeDrivingLicenseDTO dto = unitOfWork.EmployeeDrivingLicensesRepository.GetByID(id).ToDTO(); return(Ok(dto)); } catch (NotFoundException nfe) { return(NotFound()); } catch (ConflictException ce) { return(Conflict()); } catch (Exception e) { return(BadRequest(e.Message)); } }
public void DeleteLicense_ShouldDeleteAndReturnOk() { var licensesTestData = new List <EmployeeDrivingLicense>() { new EmployeeDrivingLicense { Id = 1, EmployeeId = 2 }, new EmployeeDrivingLicense { Id = 2, Deleted = true, EmployeeId = 2 }, new EmployeeDrivingLicense { Id = 3, EmployeeId = 3 } }; var licenses = MockHelper.MockDbSet(licensesTestData); licenses.Setup(d => d.Find(It.IsAny <object>())).Returns <object[]>((keyValues) => { return(licenses.Object.SingleOrDefault(product => product.Id == (int)keyValues.Single())); }); var dbContext = new Mock <IAppDbContext>(); dbContext.Setup(m => m.EmployeeDrivingLicenses).Returns(licenses.Object); dbContext.Setup(d => d.Set <EmployeeDrivingLicense>()).Returns(licenses.Object); var factory = new Mock <IDbContextFactory>(); factory.Setup(m => m.CreateDbContext()).Returns(dbContext.Object); EmployeeDrivingLicense license = new EmployeeDrivingLicense { Id = 3, EmployeeId = 3, Category = "b", IssuedBy = "Wow" }; var controller = new EmployeeDrivingLicensesController(factory.Object); var result = controller.DeleteEmployeeDrivingLicense(3) as OkNegotiatedContentResult <EmployeeDrivingLicenseDTO>; Assert.IsNotNull(result); Assert.AreEqual(3, result.Content.Id); Assert.AreEqual(3, result.Content.EmployeeId); }
public void PostLicense_ShoulAddLicense() { var licensesTestData = new List <EmployeeDrivingLicense>() { new EmployeeDrivingLicense { Id = 1, EmployeeId = 2 }, new EmployeeDrivingLicense { Id = 2, Deleted = true, EmployeeId = 2 }, new EmployeeDrivingLicense { Id = 3, EmployeeId = 3 } }; var licenses = MockHelper.MockDbSet(licensesTestData); licenses.Setup(d => d.Find(It.IsAny <object>())).Returns <object[]>((keyValues) => { return(licenses.Object.SingleOrDefault(product => product.Id == (int)keyValues.Single())); }); licenses.Setup(d => d.Add(It.IsAny <EmployeeDrivingLicense>())).Returns <EmployeeDrivingLicense>((contact) => { licensesTestData.Add(contact); licenses = MockHelper.MockDbSet(licensesTestData); return(contact); }); var dbContext = new Mock <IAppDbContext>(); dbContext.Setup(m => m.EmployeeDrivingLicenses).Returns(licenses.Object); dbContext.Setup(d => d.Set <EmployeeDrivingLicense>()).Returns(licenses.Object); dbContext.Setup(d => d.ExecuteStoredProcedure <int>(It.IsAny <string>(), It.IsAny <object[]>())) .Returns <string, object[]>((query, parameters) => { List <int> list = new List <int>(); if (query.Contains("NewTableId")) { int i = licenses.Object.Max(d => d.Id) + 1; list.Add(i); } else { list.Add(0); } return(list); }); var factory = new Mock <IDbContextFactory>(); factory.Setup(m => m.CreateDbContext()).Returns(dbContext.Object); EmployeeDrivingLicense license = new EmployeeDrivingLicense { Id = 0, EmployeeId = 3, Category = "b", IssuedBy = "Wow" }; var controller = new EmployeeDrivingLicensesController(factory.Object); var result = controller.PostEmployeeDrivingLicense(license.ToDTO()) as CreatedAtRouteNegotiatedContentResult <EmployeeDrivingLicenseDTO>; Assert.IsNotNull(result); Assert.AreEqual(4, result.Content.Id); Assert.AreEqual(3, result.Content.EmployeeId); Assert.AreEqual("b", result.Content.Category); Assert.AreEqual("Wow", result.Content.IssuedBy); }