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