2020-05-24 05:10:00 +01:00
|
|
|
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 DocumentCompaniesService
|
|
|
|
|
{
|
|
|
|
|
readonly MarechaiContext _context;
|
|
|
|
|
|
|
|
|
|
public DocumentCompaniesService(MarechaiContext context) => _context = context;
|
|
|
|
|
|
|
|
|
|
public async Task<List<DocumentCompanyViewModel>> GetAsync() => await _context.
|
|
|
|
|
DocumentCompanies.OrderBy(c => c.Name).
|
|
|
|
|
Select(d => new DocumentCompanyViewModel
|
|
|
|
|
{
|
|
|
|
|
Id = d.Id, Name = d.Name,
|
|
|
|
|
Company = d.Company.Name,
|
|
|
|
|
CompanyId = d.CompanyId
|
|
|
|
|
}).ToListAsync();
|
2020-05-24 21:00:41 +01:00
|
|
|
|
2020-05-27 20:39:08 +01:00
|
|
|
public async Task<DocumentCompanyViewModel> GetAsync(int id) =>
|
|
|
|
|
await _context.DocumentCompanies.Where(d => d.Id == id).Select(d => new DocumentCompanyViewModel
|
|
|
|
|
{
|
|
|
|
|
Id = d.Id, Name = d.Name, CompanyId = d.CompanyId
|
|
|
|
|
}).FirstOrDefaultAsync();
|
|
|
|
|
|
|
|
|
|
public async Task UpdateAsync(DocumentCompanyViewModel viewModel)
|
|
|
|
|
{
|
|
|
|
|
DocumentCompany model = await _context.DocumentCompanies.FindAsync(viewModel.Id);
|
|
|
|
|
|
|
|
|
|
if(model is null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
model.CompanyId = viewModel.CompanyId;
|
|
|
|
|
model.Name = viewModel.Name;
|
|
|
|
|
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
}
|
2020-05-25 20:15:17 +01:00
|
|
|
|
2020-05-27 22:40:59 +01:00
|
|
|
public async Task<int> CreateAsync(DocumentCompanyViewModel viewModel)
|
|
|
|
|
{
|
|
|
|
|
var model = new DocumentCompany
|
|
|
|
|
{
|
|
|
|
|
CompanyId = viewModel.CompanyId, Name = viewModel.Name
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await _context.DocumentCompanies.AddAsync(model);
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
return model.Id;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-24 21:00:41 +01:00
|
|
|
public async Task DeleteAsync(int id)
|
|
|
|
|
{
|
|
|
|
|
DocumentCompany item = await _context.DocumentCompanies.FindAsync(id);
|
|
|
|
|
|
|
|
|
|
if(item is null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_context.DocumentCompanies.Remove(item);
|
|
|
|
|
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
}
|
2020-05-24 05:10:00 +01:00
|
|
|
}
|
|
|
|
|
}
|