| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,25 +2,25 @@ | |||
| 2 | 2 | { | |
| 3 | 3 | "id": 1, | |
| 4 | 4 | "src": "https://i.pinimg.com/originals/c9/ea/65/c9ea654eb3a7398b1f702c758c1c4206.jpg", | |
| 5 | - "title": "Акция", | ||
| 5 | + "title": "Акция 1", | ||
| 6 | 6 | "text": "Такой акции вы еще не видели 1" | |
| 7 | 7 | }, | |
| 8 | 8 | { | |
| 9 | 9 | "id": 2, | |
| 10 | 10 | "src": "https://i.pinimg.com/originals/c9/ea/65/c9ea654eb3a7398b1f702c758c1c4206.jpg", | |
| 11 | - "title": "Акция", | ||
| 11 | + "title": "Акция 2", | ||
| 12 | 12 | "text": "Такой акции вы еще не видели 2" | |
| 13 | 13 | }, | |
| 14 | 14 | { | |
| 15 | 15 | "id": 3, | |
| 16 | 16 | "src": "https://i.pinimg.com/originals/c9/ea/65/c9ea654eb3a7398b1f702c758c1c4206.jpg", | |
| 17 | - "title": "Акция", | ||
| 17 | + "title": "Акция 3", | ||
| 18 | 18 | "text": "Такой акции вы еще не видели 3" | |
| 19 | 19 | }, | |
| 20 | 20 | { | |
| 21 | - "id": 5, | ||
| 21 | + "id": 4, | ||
| 22 | 22 | "src": "https://i.pinimg.com/originals/c9/ea/65/c9ea654eb3a7398b1f702c758c1c4206.jpg", | |
| 23 | - "title": "Акция", | ||
| 24 | - "text": "Такой акции вы еще не видели 5" | ||
| 23 | + "title": "Акция 4", | ||
| 24 | + "text": "Такой акции вы еще не видели 4" | ||
| 25 | 25 | } | |
| 26 | 26 | ] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,7 +8,7 @@ export interface FileAccessor { | |||
| 8 | 8 | ||
| 9 | 9 | @Injectable() | |
| 10 | 10 | export class FileService<I> { | |
| 11 | - private readonly filePath = path.resolve(__dirname); | ||
| 11 | + protected readonly filePath = path.resolve(__dirname); | ||
| 12 | 12 | ||
| 13 | 13 | constructor(filePath?: string) { | |
| 14 | 14 | if (filePath) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,68 +10,45 @@ export class StocksService { | |||
| 10 | 10 | constructor(private fileService: FileService<Stock[]>) {} | |
| 11 | 11 | ||
| 12 | 12 | create(createStockDto: CreateStockDto) { | |
| 13 | - try { | ||
| 14 | - const stocks = this.fileService.read(); | ||
| 13 | + const stocks = this.fileService.read(); | ||
| 15 | 14 | ||
| 16 | - const stock = { ...createStockDto, id: stocks.length + 2 }; | ||
| 15 | + // для простоты новый id = текущее количество карточек + 1 | ||
| 16 | + const stock = { ...createStockDto, id: stocks.length + 1 }; | ||
| 17 | 17 | ||
| 18 | - this.fileService.add(stock); | ||
| 19 | - } catch (e) { | ||
| 20 | - console.error(`Error: ${e}`); | ||
| 21 | - } | ||
| 18 | + this.fileService.add(stock); | ||
| 22 | 19 | } | |
| 23 | 20 | ||
| 24 | 21 | findAll(title?: string): Stock[] { | |
| 25 | - try { | ||
| 26 | - const stocks = this.fileService.read(); | ||
| 22 | + const stocks = this.fileService.read(); | ||
| 27 | 23 | ||
| 28 | - return title | ||
| 29 | - ? stocks.filter((stock) => | ||
| 30 | - stock.title.toLowerCase().includes(title.toLowerCase()), | ||
| 31 | - ) | ||
| 32 | - : stocks; | ||
| 33 | - } catch (e) { | ||
| 34 | - console.error(`Error: ${e}`); | ||
| 35 | - | ||
| 36 | - return []; | ||
| 37 | - } | ||
| 24 | + return title | ||
| 25 | + ? stocks.filter((stock) => | ||
| 26 | + stock.title.toLowerCase().includes(title.toLowerCase()), | ||
| 27 | + ) | ||
| 28 | + : stocks; | ||
| 38 | 29 | } | |
| 39 | 30 | ||
| 40 | 31 | findOne(id: number): Stock | null { | |
| 41 | - try { | ||
| 42 | - const stocks = this.fileService.read(); | ||
| 43 | - | ||
| 44 | - return stocks.find((stock) => stock.id === id) ?? null; | ||
| 45 | - } catch (e) { | ||
| 46 | - console.error(`Error: ${e}`); | ||
| 32 | + const stocks = this.fileService.read(); | ||
| 47 | 33 | ||
| 48 | - return null; | ||
| 49 | - } | ||
| 34 | + return stocks.find((stock) => stock.id === id) ?? null; | ||
| 50 | 35 | } | |
| 51 | 36 | ||
| 52 | 37 | update(id: number, updateStockDto: UpdateStockDto): void { | |
| 53 | - try { | ||
| 54 | - const stocks = this.fileService.read(); | ||
| 38 | + const stocks = this.fileService.read(); | ||
| 55 | 39 | ||
| 56 | - const updatedStocks = stocks.map((stock) => | ||
| 57 | - stock.id === id ? { ...stock, ...updateStockDto } : stock, | ||
| 58 | - ); | ||
| 40 | + const updatedStocks = stocks.map((stock) => | ||
| 41 | + stock.id === id ? { ...stock, ...updateStockDto } : stock, | ||
| 42 | + ); | ||
| 59 | 43 | ||
| 60 | - this.fileService.write(updatedStocks); | ||
| 61 | - } catch (e) { | ||
| 62 | - console.error(`Error: ${e}`); | ||
| 63 | - } | ||
| 44 | + this.fileService.write(updatedStocks); | ||
| 64 | 45 | } | |
| 65 | 46 | ||
| 66 | 47 | remove(id: number): void { | |
| 67 | - try { | ||
| 68 | - const filteredStocks = this.fileService | ||
| 69 | - .read() | ||
| 70 | - .filter((stock) => stock.id !== id); | ||
| 48 | + const filteredStocks = this.fileService | ||
| 49 | + .read() | ||
| 50 | + .filter((stock) => stock.id !== id); | ||
| 71 | 51 | ||
| 72 | - this.fileService.write(filteredStocks); | ||
| 73 | - } catch (e) { | ||
| 74 | - console.error(`Error: ${e}`); | ||
| 75 | - } | ||
| 52 | + this.fileService.write(filteredStocks); | ||
| 76 | 53 | } | |
| 77 | 54 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments