generated from admin/template
linq
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using PokedexApi.Models;
|
||||
using System.Linq;
|
||||
|
||||
namespace PokedexApi.Storage;
|
||||
|
||||
@@ -25,11 +26,8 @@ public class PokemonStorage
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
foreach (var p in _pokemons)
|
||||
{
|
||||
if (p.Id == id) return p;
|
||||
}
|
||||
return null;
|
||||
// поиск покемона
|
||||
return _pokemons.FirstOrDefault(p => p.Id == id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,14 +35,11 @@ public class PokemonStorage
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var result = new List<Pokemon>();
|
||||
foreach (var p in _pokemons)
|
||||
{
|
||||
if (type != null && p.Type != type) continue;
|
||||
if (minLevel != null && p.Level < minLevel) continue;
|
||||
result.Add(p);
|
||||
}
|
||||
return result;
|
||||
// фильтрация
|
||||
return _pokemons
|
||||
.Where(p => (type == null || p.Type == type) &&
|
||||
(minLevel == null || p.Level >= minLevel))
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,15 +47,10 @@ public class PokemonStorage
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var result = new List<Pokemon>();
|
||||
foreach (var p in _pokemons)
|
||||
{
|
||||
if (p.Name.ToLower().Contains(query.ToLower()))
|
||||
{
|
||||
result.Add(p);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
// поиск по имени через линк с учетом регистра
|
||||
return _pokemons
|
||||
.Where(p => p.Name.Contains(query, StringComparison.OrdinalIgnoreCase))
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,11 +58,9 @@ public class PokemonStorage
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
int newId = 1;
|
||||
foreach (var p in _pokemons)
|
||||
{
|
||||
if (p.Id >= newId) newId = p.Id + 1;
|
||||
}
|
||||
// вычисление нового id через линк
|
||||
int newId = _pokemons.Any() ? _pokemons.Max(p => p.Id) + 1 : 1;
|
||||
|
||||
var pokemon = new Pokemon(newId, input.Name, input.Type, input.Level);
|
||||
_pokemons.Add(pokemon);
|
||||
return pokemon;
|
||||
@@ -83,16 +71,14 @@ public class PokemonStorage
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
for (int i = 0; i < _pokemons.Count; i++)
|
||||
{
|
||||
if (_pokemons[i].Id == id)
|
||||
{
|
||||
var updated = new Pokemon(id, input.Name, input.Type, input.Level);
|
||||
_pokemons[i] = updated;
|
||||
return updated;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
// поиск обновляемого элемента
|
||||
var existing = _pokemons.FirstOrDefault(p => p.Id == id);
|
||||
if (existing == null) return null;
|
||||
|
||||
int index = _pokemons.IndexOf(existing);
|
||||
var updated = new Pokemon(id, input.Name, input.Type, input.Level);
|
||||
_pokemons[index] = updated;
|
||||
return updated;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,17 +86,14 @@ public class PokemonStorage
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
for (int i = 0; i < _pokemons.Count; i++)
|
||||
{
|
||||
var existing = _pokemons[i];
|
||||
if (existing.Id == id)
|
||||
{
|
||||
var updated = new Pokemon(existing.Id, existing.Name, existing.Type, level);
|
||||
_pokemons[i] = updated;
|
||||
return updated;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
// поиск покемона для изменения уровня
|
||||
var existing = _pokemons.FirstOrDefault(p => p.Id == id);
|
||||
if (existing == null) return null;
|
||||
|
||||
int index = _pokemons.IndexOf(existing);
|
||||
var updated = new Pokemon(existing.Id, existing.Name, existing.Type, level);
|
||||
_pokemons[index] = updated;
|
||||
return updated;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,15 +101,9 @@ public class PokemonStorage
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
Pokemon? found = null;
|
||||
foreach (var p in _pokemons)
|
||||
{
|
||||
if (p.Id == id)
|
||||
{
|
||||
found = p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// поиск элемента для удаления
|
||||
var found = _pokemons.FirstOrDefault(p => p.Id == id);
|
||||
|
||||
if (found != null)
|
||||
{
|
||||
_pokemons.Remove(found);
|
||||
|
||||
Reference in New Issue
Block a user