public override void GetPage_CallsWithGoodParams_FillsPageInfoProperty() { BusinessTripService bts = GetNewService(); bts.NumberOfObjectsPerPage = 2; BusinessTripDTO[] col = new BusinessTripDTO[] { new BusinessTripDTO() { Id = 1 }, new BusinessTripDTO() { Id = 2 }, new BusinessTripDTO() { Id = 3 } }; bts.GetPage(col, 1); Assert.AreEqual(1, bts.PageInfo.PageNumber); Assert.AreEqual(2, bts.PageInfo.PageSize); Assert.AreEqual(col.Length, bts.PageInfo.TotalItems); }
public async Task EditAsync_IdsParameterContainsThreeDifferentValue_CallsFindByIdAsyncMethodThreeTimes() { Mock <IUnitOfWork> mock = new Mock <IUnitOfWork>(); mock.Setup(m => m.BusinessTrips.FindByIdAsync(It.IsAny <int>())).ReturnsAsync(new BusinessTrip()); mock.Setup(m => m.Employees.FindByIdAsync(It.IsAny <int>())).ReturnsAsync(new Employee()); BusinessTripService bts = GetNewService(mock.Object); BusinessTripDTO item = new BusinessTripDTO { Name = "01.09.2018_021", DateStart = new DateTime(2018, 8, 10), DateEnd = new DateTime(2018, 8, 20), Destination = "Moscow", Purpose = "Seminar", Employees = new EmployeeDTO[] { new EmployeeDTO { Id = 1 }, new EmployeeDTO { Id = 2 }, new EmployeeDTO { Id = 2 }, new EmployeeDTO { Id = 3 } } }; await bts.EditAsync(item); mock.Verify(m => m.Employees.FindByIdAsync(It.IsAny <int>()), Times.Exactly(3)); }
public void CreateBusinessTripSubsistence(BusinessTrip trip, BusinessTripDTO businessTrip) { Subsistence sub = repo.Subsistences.Create(new Subsistence() { StartDate = DateExtensions.ParseAppString(businessTrip.Subsistence.StartDate), EndDate = DateExtensions.ParseAppString(businessTrip.Subsistence.EndDate), City = businessTrip.Subsistence.City, Country = repo.Dictionaries.GetCountry(businessTrip.Subsistence.CountryId) }); List <SubsistenceDay> days = new List <SubsistenceDay>(); foreach (SubsistenceDayDTO dayDto in businessTrip.Subsistence.Days) { days.Add(new SubsistenceDay() { Amount = dayDto.Amount, AmountPLN = dayDto.AmountPLN, ExchangeRate = dayDto.ExchangeRate, Breakfast = dayDto.Breakfast, Date = DateExtensions.ParseAppString(dayDto.Date), Dinner = dayDto.Dinner, Supper = dayDto.Supper, Night = dayDto.Night, Subsistence = sub, Diet = dayDto.Diet, IsForeign = dayDto.IsForeign }); } repo.SubsistenceDays.CreateSet(days); trip.Subsistence = sub; }
public void UpdateBusinessTripMileageAllowances(BusinessTrip trip, BusinessTripDTO businessTripDto) { if (businessTripDto != null) { foreach (MileageAllowance allowance in trip.MileageAllowances .Where(m => businessTripDto.MileageAllowances .Any(mdto => mdto.id == m.Id))) { //Update mileage allowances that exist both in database and in dto MileageAllowanceDTO mdto = businessTripDto.MileageAllowances.First(m => m.id == allowance.Id); allowance.Type = repo.Dictionaries.GetVehicleType(mdto.VehicleTypeId); allowance.Date = mdto.Date.ParseAppString(); allowance.Amount = mdto.Amount; allowance.Distance = mdto.Distance; allowance.Notes = mdto.Notes; } //Remove those that exist in db but don't exist in dto repo.MileageAllowances.RemoveSet(trip.MileageAllowances .Where(m => !businessTripDto.MileageAllowances .Any(mdto => mdto.id == m.Id))); foreach (MileageAllowanceDTO milDto in businessTripDto.MileageAllowances.Where(mdto => !trip.MileageAllowances.Any(ma => ma.Id == mdto.id))) { //Add those that exist in dto but don't exist in db MileageAllowance allowance = new MileageAllowance(); allowance.Trip = trip; allowance.Date = milDto.Date.ParseAppString(); allowance.Amount = milDto.Amount; allowance.Distance = milDto.Distance; allowance.Type = repo.Dictionaries.GetVehicleType(milDto.VehicleTypeId); allowance.Notes = milDto.Notes; trip.MileageAllowances.Add(allowance); } } }
public override void GetPage_RequestedPageMoreThanTotalPages_ReturnsLastPage() { BusinessTripService bts = GetNewService(); bts.NumberOfObjectsPerPage = 3; BusinessTripDTO[] col = new BusinessTripDTO[] { new BusinessTripDTO() { Id = 1 }, new BusinessTripDTO() { Id = 2 }, new BusinessTripDTO() { Id = 3 } }; bts.GetPage(col, 2); int totalPages = bts.PageInfo.TotalPages; // 1 Assert.AreEqual(totalPages, bts.PageInfo.PageNumber); }
public IHttpActionResult UpdateBusinessTrip(BusinessTripDTO businessTrip) { try { BusinessTrip trip = this.tasks.BusinessTripsTasks.GetBusinessTrip(businessTrip.Id.Value); if (trip.UserId != this.User.Identity.GetUserId <int>()) { return(Unauthorized()); } if (this.ModelState.IsValid) { if (!businessTrip.Id.HasValue) { return(BadRequest("Brak Id Delegacji")); } this.tasks.BusinessTripsTasks.UpdateBusinessTrip(businessTrip, this.UserName); return(Ok("Delegacja została zaktualizowana")); } else { return(BadRequest(this.ModelState)); } } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public BusinessTripSearchItemDTO Clone(int businessTripId) { BusinessTrip trip = this.tasks.BusinessTripsTasks.GetBusinessTrip(businessTripId); BusinessTripDTO dto = trip.MapToDTO(); dto.Title += " (kopia)"; int createdId = this.tasks.BusinessTripsTasks.CreateNewBusinessTrip(dto, this.UserName); return(this.tasks.BusinessTripsTasks.GetBusinessTrip(createdId).MapToSearchItem()); }
private async Task <ActionResult> GetViewAsync(int?id, string viewName) { try { BusinessTripDTO btDto = await _businessTripService.FindByIdAsync(id); BusinessTripViewModel bt = MapDTOWithViewModel(btDto); return(View(viewName, bt)); } catch (ValidationException ex) { _logger.Warn(ex.Message); return(View("Error", new string[] { ex.Message })); } }
public void UpdateBusinessTripExpenses(BusinessTrip trip, BusinessTripDTO businessTripDto) { if (businessTripDto.Expenses != null) { foreach (Expense expense in trip.Expenses .Where(e => businessTripDto.Expenses .Any(edto => edto.ExpenseId == e.Id))) { //Update expenses that exist both in database and in dto ExpenseDTO edto = businessTripDto.Expenses.First(expenseDto => expenseDto.ExpenseId == expense.Id); expense.Type = repo.Dictionaries.GetExpenseType(edto.ExpenseTypeId); expense.Date = edto.Date.ParseAppString(); expense.City = edto.City; expense.Amount = edto.Amount; expense.AmountPLN = edto.AmountPLN; //expense.CountryId = expDto.CountryId; expense.Country = repo.Dictionaries.GetCountry(edto.CountryId); expense.CurrencyCode = edto.CurrencyCode; expense.ExchangeRate = edto.ExchangeRate; expense.ExchangeRateModifiedByUser = edto.ExchangeRateModifiedByUser; expense.VATRate = edto.VATRate; expense.Notes = edto.Notes; expense.DocumentType = repo.Dictionaries.GetExpenseDocumentType(edto.ExpenseDocumentTypeId); } //Remove those that exist in db but don't exist in dto repo.Expenses.RemoveSet(trip.Expenses .Where(e => !businessTripDto.Expenses .Any(edto => edto.ExpenseId == e.Id))); foreach (ExpenseDTO expDto in businessTripDto.Expenses.Where(edto => !trip.Expenses.Any(exp => exp.Id == edto.ExpenseId))) { //Add those that exist in dto but don't exist in db Expense expense = new Expense(); expense.Trip = trip; expense.Type = repo.Dictionaries.GetExpenseType(expDto.ExpenseTypeId); expense.Date = expDto.Date.ParseAppString(); expense.City = expDto.City; expense.Amount = expDto.Amount; expense.AmountPLN = expDto.AmountPLN; //expense.CountryId = expDto.CountryId; expense.Country = repo.Dictionaries.GetCountry(expDto.CountryId); expense.CurrencyCode = expDto.CurrencyCode; expense.ExchangeRate = expDto.ExchangeRate; expense.ExchangeRateModifiedByUser = expDto.ExchangeRateModifiedByUser; expense.VATRate = expDto.VATRate; expense.Notes = expDto.Notes; expense.DoNotReturn = expDto.DoNotReturn; expense.DocumentType = repo.Dictionaries.GetExpenseDocumentType(expDto.ExpenseDocumentTypeId); trip.Expenses.Add(expense); } } }
public override async Task FindByIdAsync_IdEqualTo2_ReturnsObjectWithIdEqualTo2() { Mock <IUnitOfWork> mock = new Mock <IUnitOfWork>(); mock.Setup(m => m.BusinessTrips.FindByIdAsync(It.IsAny <int>())).ReturnsAsync((int item_id) => new BusinessTrip() { Id = item_id }); BusinessTripService bts = GetNewService(mock.Object); BusinessTripDTO result = await bts.FindByIdAsync(2); Assert.AreEqual(2, result.Id); }
public void EditAsync_DateStartPropertyMoreThanDateEndProperty_Throws() { BusinessTripService bts = GetNewService(); BusinessTripDTO item = new BusinessTripDTO { Name = "01.09.2018_021", DateStart = new DateTime(2018, 8, 20), DateEnd = new DateTime(2018, 8, 10), Destination = "Moscow", Purpose = "Seminar" }; Exception ex = Assert.CatchAsync(async() => await bts.EditAsync(item)); StringAssert.Contains("Дата окончания командировки не должна быть до даты начала", ex.Message); }
public async Task <ActionResult> Create(BusinessTripViewModel bt) { try { BusinessTripDTO btDto = MapViewModelWithDTO(bt); await(_businessTripService as BusinessTripService).CreateAsync(btDto); return(RedirectToAction("Index")); } catch (ValidationException ex) { _logger.Warn(ex.Message); ModelState.AddModelError(ex.Property, ex.Message); } ViewBag.Employees = await GetSelectListEmployeesAsync(); return(View("Create", bt)); }
public void EditAsync_DestinationPropertyIsNull_Throws() { BusinessTripService bts = GetNewService(); BusinessTripDTO item = new BusinessTripDTO { Name = "01.09.2018_021", DateStart = new DateTime(2018, 8, 10), DateEnd = new DateTime(2018, 8, 20), Destination = null, Purpose = "Seminar" }; Exception ex = Assert.CatchAsync(async() => await bts.EditAsync(item)); StringAssert.Contains("Требуется ввести место назначения", ex.Message); }
public void EditAsync_PurposePropertyIsNull_Throws() { BusinessTripService bts = GetNewService(); BusinessTripDTO item = new BusinessTripDTO { Name = "01.09.2018_021", DateStart = new DateTime(2018, 8, 10), DateEnd = new DateTime(2018, 8, 20), Destination = "Moscow", Purpose = null }; Exception ex = Assert.CatchAsync(async() => await bts.EditAsync(item)); StringAssert.Contains("Требуется ввести цель командировки", ex.Message); }
private BusinessTripViewModel MapDTOWithViewModel(BusinessTripDTO btDTO) { Mapper.Initialize(cfg => { cfg.CreateMap <BusinessTripDTO, BusinessTripViewModel>(); cfg.CreateMap <EmployeeDTO, EmployeeViewModel>() .ForMember(e => e.AnnualLeaves, opt => opt.Ignore()) .ForMember(e => e.Birth, opt => opt.Ignore()) .ForMember(e => e.BusinessTrips, opt => opt.Ignore()) .ForMember(e => e.Contacts, opt => opt.Ignore()) .ForMember(e => e.Education, opt => opt.Ignore()) .ForMember(e => e.Passport, opt => opt.Ignore()) .ForMember(e => e.Post, opt => opt.Ignore()); }); BusinessTripViewModel bt = Mapper.Map <BusinessTripDTO, BusinessTripViewModel>(btDTO); return(bt); }
public async Task CreateAsync_CallsWithGoodParams_CallsCreateMethodOnсe() { Mock <IUnitOfWork> mock = new Mock <IUnitOfWork>(); mock.Setup(m => m.BusinessTrips.Create(It.IsAny <BusinessTrip>())); BusinessTripService bts = GetNewService(mock.Object); BusinessTripDTO item = new BusinessTripDTO { Name = "01.09.2018_021", DateStart = new DateTime(2018, 8, 10), DateEnd = new DateTime(2018, 8, 20), Destination = "Moscow", Purpose = "Seminar" }; await bts.CreateAsync(item); mock.Verify(m => m.BusinessTrips.Create(It.IsAny <BusinessTrip>()), Times.Once()); }
public override async Task EditAsync_CallsWithGoodParams_CallsSaveAsyncMethodOnсe() { Mock <IUnitOfWork> mock = new Mock <IUnitOfWork>(); mock.Setup(m => m.BusinessTrips.FindByIdAsync(It.IsAny <int>())).ReturnsAsync(new BusinessTrip()); BusinessTripService bts = GetNewService(mock.Object); BusinessTripDTO item = new BusinessTripDTO { Name = "01.09.2018_021", DateStart = new DateTime(2018, 8, 10), DateEnd = new DateTime(2018, 8, 20), Destination = "Moscow", Purpose = "Seminar" }; await bts.EditAsync(item); mock.Verify(m => m.SaveAsync(), Times.Once); }
public void UpdateBusinessTrip(BusinessTripDTO businessTripDto, string userName) { BusinessTrip trip = repo.BusinessTrips.GetById(businessTripDto.Id.Value); User user = repo.Users.UsersQueryable.FirstOrDefault(u => u.UserName == userName); trip.Title = businessTripDto.Title; trip.Date = businessTripDto.Date.ParseAppString(); trip.BusinessReason = businessTripDto.BusinessReason; trip.BusinessPurpose = businessTripDto.BusinessPurpose; trip.Notes = businessTripDto.Notes; trip.User = user; UpdateBusinessTripExpenses(trip, businessTripDto); CreateUpdateBusinessTripSubsistences(trip, businessTripDto); UpdateBusinessTripMileageAllowances(trip, businessTripDto); this.repo.SaveChanges(); }
public IHttpActionResult CreateBusinessTrip(BusinessTripDTO businessTrip) { try { if (this.ModelState.IsValid) { this.tasks.BusinessTripsTasks.CreateNewBusinessTrip(businessTrip, this.UserName); return(Ok("Delegacja została stworzona")); } else { return(BadRequest(this.ModelState)); } } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public void CreateUpdateBusinessTripSubsistences(BusinessTrip trip, BusinessTripDTO businessTripDto) { Subsistence sub = null; //remove existing days, if they exist if (businessTripDto.Subsistence != null && businessTripDto.Subsistence.Id != null) { sub = repo.Subsistences.GetById(businessTripDto.Subsistence.Id.Value); var currentDays = repo.SubsistenceDays.GetForsubsistence(businessTripDto.Subsistence.Id.Value); repo.SubsistenceDays.Remove(currentDays); List <SubsistenceDay> newDays = new List <SubsistenceDay>(); foreach (SubsistenceDayDTO dayDto in businessTripDto.Subsistence.Days) { newDays.Add(new SubsistenceDay() { Amount = dayDto.Amount, AmountPLN = dayDto.AmountPLN, ExchangeRate = dayDto.ExchangeRate, Breakfast = dayDto.Breakfast, Date = DateExtensions.ParseAppString(dayDto.Date), Dinner = dayDto.Dinner, Supper = dayDto.Supper, Night = dayDto.Night, Subsistence = sub }); } repo.SubsistenceDays.CreateSet(newDays); } else if (businessTripDto.Subsistence != null) { CreateBusinessTripSubsistence(trip, businessTripDto); } }
public override void GetPage_CallsExistingPage_ReturnsSpecifiedPage() { BusinessTripService bts = GetNewService(); bts.NumberOfObjectsPerPage = 3; BusinessTripDTO[] col = new BusinessTripDTO[] { new BusinessTripDTO() { Id = 1, Name = "01.09.2018_021" }, new BusinessTripDTO() { Id = 2, Name = "02.09.2018_022" }, new BusinessTripDTO() { Id = 3, Name = "03.09.2018_023" }, new BusinessTripDTO() { Id = 4, Name = "04.09.2018_024" }, new BusinessTripDTO() { Id = 5, Name = "05.09.2018_025" } }; BusinessTripDTO[] result = bts.GetPage(col, 2).ToArray(); Assert.AreEqual(2, result.Length); Assert.AreEqual(4, result[0].Id); Assert.AreEqual(5, result[1].Id); Assert.AreEqual("04.09.2018_024", result[0].Name); Assert.AreEqual("05.09.2018_025", result[1].Name); }
public int CreateNewBusinessTrip(BusinessTripDTO businessTrip, string userName) { User user = repo.Users.UsersQueryable.FirstOrDefault(u => u.UserName == userName); BusinessTrip trip = repo.BusinessTrips.Create(new BusinessTrip() { Title = businessTrip.Title, Date = businessTrip.Date.ParseAppString(), BusinessReason = businessTrip.BusinessReason, BusinessPurpose = businessTrip.BusinessPurpose, Notes = businessTrip.Notes, User = user }); if (businessTrip.Subsistence != null) { CreateBusinessTripSubsistence(trip, businessTrip); } List <Expense> expenses = new List <Expense>(); if (businessTrip.Expenses != null) { foreach (ExpenseDTO expDto in businessTrip.Expenses) { Expense expense = new Expense(); expense.Trip = trip; expense.Type = repo.Dictionaries.GetExpenseType(expDto.ExpenseTypeId); expense.Date = expDto.Date.ParseAppString(); expense.City = expDto.City; expense.Amount = expDto.Amount; expense.AmountPLN = expDto.AmountPLN; expense.Country = repo.Dictionaries.GetCountry(expDto.CountryId); expense.CurrencyCode = expDto.CurrencyCode; expense.ExchangeRate = expDto.ExchangeRate; CurrencyRate systemRate = currenciesTasks.GetCurrencyRateForDay(expense.CurrencyCode, expense.Date.Date); if (Math.Abs(systemRate.ExchangeRate - expense.ExchangeRate) > 0.0001) { expense.ExchangeRateModifiedByUser = true; } else { expense.ExchangeRateModifiedByUser = false; } expense.VATRate = expDto.VATRate; expense.Notes = expDto.Notes; expense.DoNotReturn = expDto.DoNotReturn; expense.DocumentType = repo.Dictionaries.GetExpenseDocumentType(expDto.ExpenseDocumentTypeId); expenses.Add(expense); } repo.Expenses.CreateSet(expenses); } if (businessTrip.MileageAllowances != null) { List <MileageAllowance> mileageAllowances = new List <MileageAllowance>(); foreach (MileageAllowanceDTO maDto in businessTrip.MileageAllowances) { MileageAllowance ma = new MileageAllowance(); ma.Trip = trip; ma.Date = maDto.Date.ParseAppString(); ma.Distance = maDto.Distance; ma.Amount = maDto.Amount; ma.Notes = maDto.Notes; ma.Type = repo.Dictionaries.GetVehicleType(maDto.VehicleTypeId); mileageAllowances.Add(ma); } repo.MileageAllowances.CreateSet(mileageAllowances); } this.repo.SaveChanges(); return(trip.Id); }