Files
marechai/Marechai/Services/SoundSynthsService.cs

102 lines
5.3 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Marechai.Database.Models;
using Marechai.ViewModels;
using Microsoft.EntityFrameworkCore;
namespace Marechai.Services
{
public class SoundSynthsService
{
readonly MarechaiContext _context;
public SoundSynthsService(MarechaiContext context) => _context = context;
public async Task<List<SoundSynthViewModel>> GetAsync() =>
await _context.SoundSynths.OrderBy(s => s.Company.Name).ThenBy(s => s.Name).ThenBy(s => s.ModelCode).
Select(s => new SoundSynthViewModel
{
Id = s.Id, Name = s.Name, CompanyId = s.Company.Id, CompanyName = s.Company.Name,
ModelCode = s.ModelCode, Introduced = s.Introduced, Voices = s.Voices,
Frequency = s.Frequency, Depth = s.Depth, SquareWave = s.SquareWave,
WhiteNoise = s.WhiteNoise, Type = s.Type
}).ToListAsync();
public async Task<List<SoundSynthViewModel>> GetByMachineAsync(int machineId) =>
await _context.SoundByMachine.Where(s => s.MachineId == machineId).Select(s => s.SoundSynth).
OrderBy(s => s.Company.Name).ThenBy(s => s.Name).ThenBy(s => s.ModelCode).
Select(s => new SoundSynthViewModel
{
Id = s.Id, Name = s.Name, CompanyId = s.Company.Id, CompanyName = s.Company.Name,
ModelCode = s.ModelCode, Introduced = s.Introduced, Voices = s.Voices,
Frequency = s.Frequency, Depth = s.Depth, SquareWave = s.SquareWave,
WhiteNoise = s.WhiteNoise, Type = s.Type
}).ToListAsync();
2020-05-24 22:03:48 +01:00
public async Task<SoundSynthViewModel> GetAsync(int id) => await _context.SoundSynths.Where(s => s.Id == id).
Select(s => new SoundSynthViewModel
{
Id = s.Id, Name = s.Name,
CompanyId = s.Company.Id,
CompanyName = s.Company.Name,
ModelCode = s.ModelCode,
Introduced = s.Introduced,
Voices = s.Voices,
Frequency = s.Frequency,
Depth = s.Depth,
SquareWave = s.SquareWave,
WhiteNoise = s.WhiteNoise,
Type = s.Type
}).FirstOrDefaultAsync();
public async Task UpdateAsync(SoundSynthViewModel viewModel)
{
SoundSynth model = await _context.SoundSynths.FindAsync(viewModel.Id);
if(model is null)
return;
model.Depth = viewModel.Depth;
model.Frequency = viewModel.Frequency;
model.Introduced = viewModel.Introduced;
model.Name = viewModel.Name;
model.Type = viewModel.Type;
model.Voices = viewModel.Voices;
model.CompanyId = viewModel.CompanyId;
model.ModelCode = viewModel.ModelCode;
model.SquareWave = viewModel.SquareWave;
model.WhiteNoise = viewModel.WhiteNoise;
await _context.SaveChangesAsync();
}
public async Task<int> CreateAsync(SoundSynthViewModel viewModel)
{
var model = new SoundSynth
{
Depth = viewModel.Depth, Frequency = viewModel.Frequency, Introduced = viewModel.Introduced,
Name = viewModel.Name, Type = viewModel.Type, Voices = viewModel.Voices,
CompanyId = viewModel.CompanyId, ModelCode = viewModel.ModelCode, SquareWave = viewModel.SquareWave,
WhiteNoise = viewModel.WhiteNoise
};
await _context.SoundSynths.AddAsync(model);
await _context.SaveChangesAsync();
return model.Id;
}
2020-05-24 22:03:48 +01:00
public async Task DeleteAsync(int id)
{
SoundSynth item = await _context.SoundSynths.FindAsync(id);
if(item is null)
return;
_context.SoundSynths.Remove(item);
await _context.SaveChangesAsync();
}
}
}