public void ReadGuests_Success_ResultIsInstanseOfExpectedTuple() { //Arrenge var filter = new GuestFilter() { Name = "abc", Take = 1, Skip = 1 }; var guests = GetTestsGuests(); var guestRepoMock = new Mock <IGuestRepository>(); guestRepoMock.Setup(r => r.ReadGuests(It.Is <GuestFilter>(f => f.Name == filter.Name && f.Take == filter.Take && f.Skip == filter.Skip))) .Returns((guests, guests.Count)); var mappingConfig = new MapperConfiguration(mc => { mc.AddProfile(new MappingProfile()); }); IMapper mapper = mappingConfig.CreateMapper(); var guestService = new GuestService(mapper, guestRepoMock.Object); //Act var result = guestService.ReadGuests(filter); //Assert Assert.IsInstanceOf <(IEnumerable <GuestViewModel>, int)>(result); }
public (IEnumerable <GuestViewModel> models, int count) ReadGuests(GuestFilter filter) { var(guests, count) = guestRepository.ReadGuests(filter); var guestModel = mapper.Map <IEnumerable <GuestViewModel> >(guests); return(guestModel, count); }
public (IEnumerable <Guest> guests, int count) ReadGuests(GuestFilter filter) { var query = context.Guests.Take(filter.Take).Skip(filter.Skip); if (!String.IsNullOrEmpty(filter.Name)) { query = query.Where(g => g.FirstName.Contains(filter.Name) || g.LastName.Contains(filter.Name)); } switch (filter.SortOrder) { case "FirstName": query = query.OrderBy(g => g.FirstName); break; case "LastName DESC": query = query.OrderByDescending(g => g.LastName); break; default: query = query.OrderBy(g => g.Id); break; } var guests = query.ToList(); return(guests, guests.Count); }
public async Task <ProcessResult <int> > CountAsync(GuestFilter filter) { IQueryable <Guest> q = context.Guests; q = SetFilter(q, filter); Func <Task <int> > action = async() => { var countItems = await q.CountAsync(); return(countItems); }; return(await Process.RunAsync(action)); }
public void Index_IsNotNull_ViewResultIsNotNull() { // arrange int pageNumber = 1; string searchString = ""; var filter = new GuestFilter(); var mock = new Mock <IGuestService>(); GuestController controller = new GuestController(mock.Object); // act var result = controller.Index(searchString, pageNumber, filter); //assert var res = result as ViewResult; Assert.IsNotNull(res); }
public async Task <IActionResult> Count(GuestFilter filter) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = await _GuestService.CountAsync(filter); if (!result.Succeeded) { return(BadRequest(result.Errors)); } return(Ok(result)); }
public IActionResult Index(string searchString, int?pageNumber, GuestFilter guestFilter) { try { ViewData["CurrentSort"] = guestFilter.SortOrder; ViewData["FirstNameSortParm"] = String.IsNullOrEmpty(guestFilter.SortOrder) ? "fName" : ""; ViewData["LastNameSortParm"] = guestFilter.SortOrder == "Last Name" ? "lName_desc" : "Last Name"; ViewData["CurrentFilter"] = searchString; guestFilter.Name = searchString; guestFilter.Take = 5; pageNumber ??= 1; guestFilter.Skip = (pageNumber.Value - 1) * guestFilter.Take; var(guests, count) = guestService.ReadGuests(guestFilter); return(View(PaginatedList <GuestViewModel> .Create(guests, count, pageNumber.Value, guestFilter.Take))); } catch (Exception) { return(BadRequest()); } }
private IQueryable <Guest> SetFilter(IQueryable <Guest> q, GuestFilter f) { if (f == null) { return(q); } if (!String.IsNullOrEmpty(f.searchString)) { q = q.Where(s => s.Name.Contains(f.searchString)); } if (f.identification != "") { q = q.Where(s => s.Identification == f.identification); } if (f.countryID != 0) { q = q.Where(s => s.CountryID == f.countryID); } return(q); }
public void Index_CallsReadGuestService_WithFilter() { //Arrange var filter = new GuestFilter() { Name = "abc", Take = 1, Skip = 1 }; var guests = GetTestsGuests(); var guestService = new Mock <IGuestService>(); guestService.Setup(s => s.ReadGuests(It.Is <GuestFilter>(f => f.Name == filter.Name && f.Take == filter.Take && f.Skip == filter.Skip))) .Returns((guests, guests.Count)); var controller = new GuestController(guestService.Object); // Act var result = controller.Index("", null, filter); //Assert guestService.Verify(s => s.ReadGuests(filter), Times.Once); }
public (IEnumerable <Guest>, int) ReadGuests(GuestFilter filter) { var guests = new List <Guest>(); var count = 0; using (var conn = new SqlConnection(connectionString)) { var cmd = new SqlCommand(); cmd.Connection = conn; var orderBy = ""; switch (filter.SortOrder) { case "fName": orderBy = "FirstName"; break; case "lName_desc": orderBy = "LastName DESC"; break; default: orderBy = "Id"; break; } var where = string.IsNullOrWhiteSpace(filter.Name) ? "" : $" WHERE FirstName LIKE '%{filter.Name}%' OR LastName LIKE '%{filter.Name}%' "; var selectCmd = $@"SELECT * FROM Guests {where} ORDER BY {orderBy} OFFSET {filter.Skip} ROWS FETCH NEXT {filter.Take} ROWS ONLY; SELECT COUNT(*) FROM Guests {where};"; logger.LogInformation(selectCmd); cmd.CommandText = selectCmd; if (conn.State != ConnectionState.Open) { conn.Open(); } using (var dr = cmd.ExecuteReader()) { while (dr.Read()) { var g = new Guest(); g.Id = dr.GetInt32("Id"); g.FirstName = dr.GetString("FirstName"); g.LastName = dr.GetString("LastName"); g.Email = dr.GetString("Email"); g.Phone = dr.GetString("Phone"); g.City = dr.GetString("City"); g.Country = dr.GetString("Country"); g.ReservationsCount = dr.IsDBNull(dr.GetOrdinal("ReservationsCount")) ? 0 : (int)dr["ReservationsCount"]; guests.Add(g); } dr.NextResult(); if (dr.Read()) { count = dr.GetInt32(0); } } } return(guests, count); }