Skip to content

Commit

Permalink
UserList add filters
Browse files Browse the repository at this point in the history
  • Loading branch information
homin12 committed Dec 13, 2024
1 parent 68709b8 commit a4d5777
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 6 deletions.
5 changes: 3 additions & 2 deletions backend/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public UserController(UserService userService, AnimeService animeService, Review
}

[HttpGet]
public async Task<List<User>> Get(string login = "", string sort = "registred_date", string order = "-1", string role = "") =>
await _userService.GetAsync(login, sort, order, role);
public async Task<List<User>> Get(string login = "", string sort = "registred_date", string order = "-1", string role = "",
string fromDate = "", string toDate = "", int minRates = 0, int maxRates = 0, int minReviews = 0, int maxReviews = 0) =>
await _userService.GetAsync(login, sort, order, role, fromDate, toDate, minRates, maxRates, minReviews, maxReviews);

[HttpGet("{id:length(24)}")]
public async Task<ActionResult<User>> Get(string id)
Expand Down
21 changes: 19 additions & 2 deletions backend/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using MongoDB.Driver;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using System.Text.RegularExpressions;

namespace AnimeCatalogApi.Services;

Expand All @@ -28,8 +29,24 @@ public UserService(
}
}

public async Task<List<User>> GetAsync(string login, string sort, string order, string role) =>
await _userCollection.Find($"{{role: {{$regex: \"{role}\"}}, login: /{login}/i }}").Sort($"{{ {sort}: {order} }}").ToListAsync();
public async Task<List<User>> GetAsync(string login, string sort, string order, string role,
string fromDate, string toDate, int minRates, int maxRates, int minReviews, int maxReviews){
if(fromDate == "")
fromDate = string.Format("{0}", DateTime.MinValue);
if(toDate == "")
toDate = string.Format("{0}", DateTime.Now);
var filter = Builders<User>.Filter.And(
Builders<User>.Filter.Regex(x => x.Role, new BsonRegularExpression(role)),
Builders<User>.Filter.Regex(x => x.Login, new BsonRegularExpression(login, "i")),
Builders<User>.Filter.AnyGte("registred_at", DateTime.Parse(fromDate)),
Builders<User>.Filter.AnyLte("registred_at", DateTime.Parse(toDate)),
Builders<User>.Filter.AnyGte("rates_count", minRates),
Builders<User>.Filter.AnyLte("rates_count", maxRates),
Builders<User>.Filter.AnyGte("reviews_count", minReviews),
Builders<User>.Filter.AnyLte("reviews_count",maxReviews)
);
return await _userCollection.Find(filter).Sort($"{{ {sort}: {order} }}").ToListAsync();
}

public async Task<User?> GetAsync(string id) =>
await _userCollection.Find(x => x.Id == id).FirstOrDefaultAsync();
Expand Down
91 changes: 89 additions & 2 deletions frontend/src/pages/UsersList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@ import { Link } from 'react-router-dom'
let base_url = 'http://localhost:5000/api/User?'
let name = ""
let admin = ""
let minRatesValue = 0
let maxRatesValue = 1000
let minValue = 0
let maxValue = 1000
let sort = new Map([
["name", ""],
["order", ""],
]);
let dates = new Map([
["from", ""],
["to", ""],
]);

const UsersList = () => {
const [users, setUsers] = useState([]);

useEffect(() => {

fetchUsers();
Expand All @@ -34,11 +41,39 @@ const UsersList = () => {
admin = ''
fetchUsers()
}
async function DateFromFilter(from) {
dates.set("from", from)
fetchUsers()
}
async function DateToFilter(to) {
dates.set("to", to)
fetchUsers()
}

const handleChangeMinValue = (e) => {
minValue = Number(e.target.value)
fetchUsers()
};
const handleChangeMaxValue = (e) => {
maxValue = Number(e.target.value)
fetchUsers()
};
const handleChangeMinRatesValue = (e) => {
minRatesValue = Number(e.target.value)
fetchUsers()
};
const handleChangeMaxRatesValue = (e) => {
maxRatesValue = Number(e.target.value)
fetchUsers()
};

const fetchUsers = async () => {
let url = base_url + name
+ "&sort=" + sort.get("name") + "&order=" + sort.get("order")
+ "&role=" + admin
+ "&role=" + admin
+ "&fromDate=" + dates.get("from") + "&toDate=" + dates.get("to")
+ "&minReviews=" + minValue + "&maxReviews=" + maxValue
+ "&minRates=" + minRatesValue + "&maxRates=" + maxRatesValue
const response = await fetch(url, {method: 'GET'});
const data = await response.json();
setUsers(data);
Expand Down Expand Up @@ -79,6 +114,58 @@ const UsersList = () => {
<label>
<input type = "checkbox" onChange={e => AdminFilter(e.target.checked, "admin" )}/> Только администрация
</label>
<div> Дата регистрации:
С <input type="date" onChange={e => DateFromFilter(e.target.value)}/>
По <input type="date" onChange={e => DateToFilter(e.target.value)}/>
</div>
<div>
<label>
Минимум обзоров: {minValue}
<input
type="range"
min="0"
max="1000"
value={minValue}
onChange={(e) => {handleChangeMinValue(e)}}
/>
</label>
</div>
<div>
<label>
Максимум обзоров: {maxValue}
<input
type="range"
min="0"
max="1000"
value={maxValue}
onChange={(e) => {handleChangeMaxValue(e)}}
/>
</label>
</div>
<div>
<label>
Минимум оценок: {minRatesValue}
<input
type="range"
min="0"
max="1000"
value={minRatesValue}
onChange={(e) => {handleChangeMinRatesValue(e)}}
/>
</label>
</div>
<div>
<label>
Максимум оценок: {maxRatesValue}
<input
type="range"
min="0"
max="1000"
value={maxRatesValue}
onChange={(e) => {handleChangeMaxRatesValue(e)}}
/>
</label>
</div>
<ul>
{users.map(user => (
<li key={user.id}>
Expand Down

0 comments on commit a4d5777

Please sign in to comment.