public OperationResult <IEnumerable <MedicineHeader> > GetMedicineList(GetMedicinesViewModel filters) { try { var medicineList = _context.Medicines.Where(x => (filters == null || string.IsNullOrEmpty(filters.SearchValue)) ? true : (x.MedicineType.Name.Contains(filters.SearchValue) || x.ContentQuantity.Contains(filters.SearchValue) || x.Id.ToString().Contains(filters.SearchValue) || x.Name.Contains(filters.SearchValue) || x.Producer.Contains(filters.SearchValue))).Select(x => new MedicineHeader { Id = x.Id, Name = x.Name, ContentQuantity = x.ContentQuantity, Producer = x.Producer, Price = x.Price }).ToList(); return(new OperationResult <IEnumerable <MedicineHeader> >(medicineList)); } catch { return(new OperationResult <IEnumerable <MedicineHeader> >( new OperationError((int)ErrorCodes.InternalServerError, ErrorResources.DatabaseError))); } }
public IActionResult GetMedicineList([FromQuery] GetMedicinesViewModel filters) { if (ModelState.IsValid) { if (filters != null) { filters.SearchValue = filters.SearchValue.TrimString(); } var result = _medicinesRepository.GetMedicineList(filters); if (result.IsSuccess) { return(Ok(result.Result)); } return(BadRequest(result.Errors)); } return(BadRequest(GlobalHelpers.ModelStateError())); }
public void GetMedicineList_CorrectDataWithFilters_ListOfMedicines() { // Arrange var filters = new GetMedicinesViewModel { SearchValue = "lek" }; mockContext.Setup(x => x.Medicines).Returns(mockMedicine.Object); var medicinesRepository = new MedicinesRepository(mockContext.Object); // Act var result = medicinesRepository.GetMedicineList(filters); // Assert Assert.IsNull(result.Errors); Assert.IsNotNull(result); Assert.AreEqual(2, result.Result.Items.Count()); Assert.AreEqual(2, result.Result.TotalCount); Assert.True(result.IsSuccess); }
public void GetMedicineList_CorrectDataWithSearchValue_ListOfMedicines() { // Arrange var searchValue = new GetMedicinesViewModel { SearchValue = "lek" }; mock.Setup(x => x.GetMedicineList(It.IsAny <GetMedicinesViewModel>())) .Returns(() => new OperationResult <IEnumerable <MedicineHeader> >(new List <MedicineHeader>(returnedMedicineList))); var medicinesController = new MedicinesController(mock.Object); // Act var result = medicinesController.GetMedicineList(searchValue) as OkObjectResult; var response = result.Value as Result <IEnumerable <MedicineHeader> >; // Assert Assert.AreEqual((int)HttpStatusCode.OK, result.StatusCode); Assert.IsNotNull(response.Items); Assert.AreEqual(2, response.TotalCount); Assert.AreEqual(2, response.Items.Count()); }