Files
marechai/Marechai/Services/GpusService.cs

137 lines
7.5 KiB
C#
Raw Permalink Normal View History

2020-06-01 02:12:50 +01:00
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2025-11-14 05:08:14 +00:00
// Copyright © 2003-2026 Natalia Portillo
2020-06-01 02:12:50 +01:00
*******************************************************************************/
2020-05-24 05:42:38 +01:00
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Marechai.Data.Dtos;
2020-05-24 05:42:38 +01:00
using Marechai.Database.Models;
using Microsoft.EntityFrameworkCore;
2025-11-13 04:05:35 +00:00
namespace Marechai.Services;
public class GpusService(MarechaiContext context)
2020-05-24 05:42:38 +01:00
{
public async Task<List<GpuDto>> GetAsync() => await context.Gpus.OrderBy(g => g.Company.Name)
2025-11-13 04:05:35 +00:00
.ThenBy(g => g.Name)
.ThenBy(g => g.Introduced)
.Select(g => new GpuDto
2025-11-13 04:05:35 +00:00
{
Id = g.Id,
Company = g.Company.Name,
Introduced = g.Introduced,
ModelCode = g.ModelCode,
Name = g.Name
})
.ToListAsync();
public async Task<List<GpuDto>> GetByMachineAsync(int machineId) => await context.GpusByMachine
2025-11-13 04:05:35 +00:00
.Where(g => g.MachineId == machineId)
.Select(g => g.Gpu)
.OrderBy(g => g.Company.Name)
.ThenBy(g => g.Name)
.Select(g => new GpuDto
2025-11-13 04:05:35 +00:00
{
Id = g.Id,
Name = g.Name,
Company = g.Company.Name,
CompanyId = g.Company.Id,
ModelCode = g.ModelCode,
Introduced = g.Introduced,
Package = g.Package,
Process = g.Process,
ProcessNm = g.ProcessNm,
DieSize = g.DieSize,
Transistors = g.Transistors
})
.ToListAsync();
public async Task<GpuDto> GetAsync(int id) => await context.Gpus.Where(g => g.Id == id)
.Select(g => new GpuDto
2025-11-13 04:05:35 +00:00
{
Id = g.Id,
Name = g.Name,
CompanyId = g.Company.Id,
ModelCode = g.ModelCode,
Introduced = g.Introduced,
Package = g.Package,
Process = g.Process,
ProcessNm = g.ProcessNm,
DieSize = g.DieSize,
Transistors = g.Transistors
})
.FirstOrDefaultAsync();
public async Task UpdateAsync(GpuDto dto, string userId)
2020-05-24 05:42:38 +01:00
{
Gpu model = await context.Gpus.FindAsync(dto.Id);
2020-05-27 19:01:11 +01:00
2025-11-13 04:05:35 +00:00
if(model is null) return;
2020-05-27 19:01:11 +01:00
model.Name = dto.Name;
model.CompanyId = dto.CompanyId;
model.ModelCode = dto.ModelCode;
model.Introduced = dto.Introduced;
model.Package = dto.Package;
model.Process = dto.Process;
model.ProcessNm = dto.ProcessNm;
model.DieSize = dto.DieSize;
model.Transistors = dto.Transistors;
2020-05-27 19:01:11 +01:00
2025-11-13 04:05:35 +00:00
await context.SaveChangesWithUserAsync(userId);
}
2020-05-25 22:57:03 +01:00
public async Task<int> CreateAsync(GpuDto dto, string userId)
2025-11-13 04:05:35 +00:00
{
var model = new Gpu
2020-05-24 21:17:14 +01:00
{
Name = dto.Name,
CompanyId = dto.CompanyId,
ModelCode = dto.ModelCode,
Introduced = dto.Introduced,
Package = dto.Package,
Process = dto.Process,
ProcessNm = dto.ProcessNm,
DieSize = dto.DieSize,
Transistors = dto.Transistors
2025-11-13 04:05:35 +00:00
};
await context.Gpus.AddAsync(model);
await context.SaveChangesWithUserAsync(userId);
return model.Id;
}
public async Task DeleteAsync(int id, string userId)
{
Gpu item = await context.Gpus.FindAsync(id);
2020-05-24 21:17:14 +01:00
2025-11-13 04:05:35 +00:00
if(item is null) return;
2020-05-24 21:17:14 +01:00
2025-11-13 04:05:35 +00:00
context.Gpus.Remove(item);
2020-05-24 21:17:14 +01:00
2025-11-13 04:05:35 +00:00
await context.SaveChangesWithUserAsync(userId);
2020-05-24 05:42:38 +01:00
}
}