public async void Get_all_errors_by_environment() { //Arrange List <ListLogErrorsViewModel> listLogErrorsViewModel = new List <ListLogErrorsViewModel>(); listLogErrorsViewModel.Add(CreateListLogErrorsViewModel(5, "Our Log Error", EEnvironment.Production, ELevel.Warning, "Source", "Details", 300, false, 1)); listLogErrorsViewModel.Add(CreateListLogErrorsViewModel(1, "Log Error", EEnvironment.Production, ELevel.Warning, "Source", "Details", 300, false, 2)); listLogErrorsViewModel.Add(CreateListLogErrorsViewModel(1, "Log Error", EEnvironment.Development, ELevel.Warning, "Source", "Details", 300, false, 3)); List <ListLogErrorsViewModel> expected = new List <ListLogErrorsViewModel>(); listLogErrorsViewModel.Add(listLogErrorsViewModel[0]); listLogErrorsViewModel.Add(listLogErrorsViewModel[1]); GetLogErrorsQueryViewModel query = new GetLogErrorsQueryViewModel { Environment = EEnvironment.Production }; Response <List <ListLogErrorsViewModel> > response = new Response <List <ListLogErrorsViewModel> >(data: expected, success: true, errors: null); _logErrorServiceMock.Setup(x => x.Get(query)) .Returns(Task.FromResult(response)); // Act var logErrorController = new LogErrorsController(_logErrorServiceMock.Object, _loggerMock.Object); var actionResult = await logErrorController.GetAll(query); var result = actionResult.Result as OkObjectResult; // Assert result.StatusCode.Should() .Be((int)HttpStatusCode.OK); var obtainedResponse = result.Value as Response <List <ListLogErrorsViewModel> >; obtainedResponse.Should() .BeEquivalentTo(response); }
public async Task <ActionResult <Response <List <ListLogErrorsViewModel> > > > GetAll([FromQuery, Optional] GetLogErrorsQueryViewModel query) { Response <List <ListLogErrorsViewModel> > model = await _logErrorService.Get(query); if (model.Success == false) { return(NotFound(model)); } return(Ok(model)); }
public async Task <Response <List <ListLogErrorsViewModel> > > Get(GetLogErrorsQueryViewModel query) { var logErrors = await _logErrorRepository.GetAllUnarchivedAsync(); if (logErrors == null) { return(new Response <List <ListLogErrorsViewModel> >( success: false, errors: new[] { "There are no errors to show" })); } var listLogErrors = logErrors .Select(x => new ListLogErrorsViewModel(environment: x.Environment, level: x.Level, source: x.Source, title: x.Title, userId: x.UserId, details: x.Details, filed: x.Filed, id: x.Id, events: CountEvents(x, logErrors))); if (query.Environment != 0) { listLogErrors = listLogErrors.Where(x => x.Environment == query.Environment); } if (query.Search != null) { Regex rx = new Regex(query.Search, RegexOptions.Compiled | RegexOptions.IgnoreCase); switch (query.SearchBy) { case 0: listLogErrors = listLogErrors.Where(x => rx.IsMatch(x.Details)); break; case ESearchBy.LEVEL: listLogErrors = listLogErrors.Where(x => rx.IsMatch(x.Level.ToFriendlyString())); break; case ESearchBy.DETAILS: listLogErrors = listLogErrors.Where(x => rx.IsMatch(x.Details)); break; case ESearchBy.SOURCE: listLogErrors = listLogErrors.Where(x => rx.IsMatch(x.Source)); break; } } switch (query.SortBy) { case 0: break; case ESortBy.FREQUENCY: listLogErrors = listLogErrors.OrderByDescending(x => x.Events); break; case ESortBy.LEVEL: listLogErrors = listLogErrors.OrderByDescending(x => x.Level); break; default: break; } return(new Response <List <ListLogErrorsViewModel> >( data: listLogErrors.ToList(), success: true, errors: null)); }