From 9bffd362462520cbecaebbe1dde3190172700f27 Mon Sep 17 00:00:00 2001 From: AlexWify Date: Wed, 28 Oct 2026 23:57:21 +0300 Subject: [PATCH] linq --- .../PokedexApi/Storage/PokemonStorage.cs | 91 +++++++------------ 1 file changed, 34 insertions(+), 57 deletions(-) diff --git a/works/lab-02/part2/PokedexApi/PokedexApi/Storage/PokemonStorage.cs b/works/lab-02/part2/PokedexApi/PokedexApi/Storage/PokemonStorage.cs index b3a347f..f661a48 100644 --- a/works/lab-02/part2/PokedexApi/PokedexApi/Storage/PokemonStorage.cs +++ b/works/lab-02/part2/PokedexApi/PokedexApi/Storage/PokemonStorage.cs @@ -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(); - 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(); - 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);