public async Task <ActionResult> Activities([FromRoute] long locationId, [FromRoute] long administratorId, [FromQuery] QueryReqModel query) { var res = await _adminService.GetActivities(locationId, _authHelpers.GetCurrentUserId().Value, administratorId, query); return(StatusCode(res.GetStatusCode(), res.Result)); }
public async Task <ServiceResponseResult> GetActivities(long locationId, long userId, long adminUserId, QueryReqModel query) { Logger.WriteInformation("Getiing admin activity data."); query.Takes = query.Takes > _appSettings.Limit ? _appSettings.Limit : query.Takes; query.OrderDirection = string.IsNullOrEmpty(query.OrderDirection) ? "asc" : query.OrderDirection.ToLower(); query.OrderBy = string.IsNullOrEmpty(query.OrderBy) ? "" : query.OrderBy.ToLower(); var userLoc = await _context.UserLocation.FirstOrDefaultAsync(x => x.UserId == adminUserId && x.LocationId == locationId); if (userLoc == null) { return(new ServiceResponseResult { Result = new { Message = $"User does not exists in this location {locationId}" }, StatusCode = System.Net.HttpStatusCode.NotFound }); } var user = await _context.User.FindAsync(adminUserId); var activityQuery = _context.UserActivity.Where(x => x.UserId == adminUserId && x.LocationId == locationId); if (query.OrderDirection == "asc") { switch (query.OrderBy.ToLower()) { case "activitytext": { activityQuery = activityQuery.OrderBy(x => x.ActivityText); break; } case "locationid": { activityQuery = activityQuery.OrderBy(x => x.LocationId); break; } case "userid": { activityQuery = activityQuery.OrderBy(x => x.UserId); break; } case "activitytime": { activityQuery = activityQuery.OrderBy(x => x.ActivityTime); break; } default: { activityQuery = activityQuery.OrderByDescending(x => x.ActivityTime); break; } } } else { switch (query.OrderBy.ToLower()) { case "activitytext": { activityQuery = activityQuery.OrderByDescending(x => x.ActivityText); break; } case "locationid": { activityQuery = activityQuery.OrderByDescending(x => x.LocationId); break; } case "userid": { activityQuery = activityQuery.OrderByDescending(x => x.UserId); break; } case "activitytime": { activityQuery = activityQuery.OrderByDescending(x => x.ActivityTime); break; } default: { activityQuery = activityQuery.OrderByDescending(x => x.ActivityTime); break; } } } activityQuery = activityQuery.Skip(query.Skips).Take(query.Takes); var result = await activityQuery.Select(x => new ActivityResult { ActivityText = x.ActivityText, ActivityTime = x.ActivityTime, Id = x.Id }).ToListAsync(); Logger.WriteInformation("Getiing admin activity completed."); return(new ServiceResponseResult { Result = new ServiceCollectionResult { TotalItems = result.Count, Items = result }, StatusCode = System.Net.HttpStatusCode.OK }); }