2020-05-21 17:23:46 +01:00
|
|
|
/******************************************************************************
|
|
|
|
|
// MARECHAI: Master repository of computing history artifacts information
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : NewsService.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// News service
|
|
|
|
|
//
|
|
|
|
|
// --[ 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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
// Copyright © 2003-2020 Natalia Portillo
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Marechai.Database;
|
|
|
|
|
using Marechai.Database.Models;
|
|
|
|
|
using Marechai.ViewModels;
|
2020-05-21 18:52:40 +01:00
|
|
|
using Microsoft.Extensions.Localization;
|
2020-05-21 17:23:46 +01:00
|
|
|
|
|
|
|
|
namespace Marechai.Services
|
|
|
|
|
{
|
|
|
|
|
public class NewsService
|
|
|
|
|
{
|
2020-05-21 18:52:40 +01:00
|
|
|
readonly MarechaiContext _context;
|
|
|
|
|
readonly IStringLocalizer<NewsService> _l;
|
2020-05-21 17:23:46 +01:00
|
|
|
|
2020-05-21 18:52:40 +01:00
|
|
|
public NewsService(MarechaiContext context, IStringLocalizer<NewsService> localizer)
|
|
|
|
|
{
|
|
|
|
|
_context = context;
|
|
|
|
|
_l = localizer;
|
|
|
|
|
}
|
2020-05-21 17:23:46 +01:00
|
|
|
|
|
|
|
|
public List<NewsViewModel> GetNews()
|
|
|
|
|
{
|
|
|
|
|
List<NewsViewModel> news = new List<NewsViewModel>();
|
|
|
|
|
|
|
|
|
|
foreach(News @new in _context.News.OrderByDescending(t => t.Date).Take(10).ToList())
|
|
|
|
|
{
|
|
|
|
|
Machine machine = _context.Machines.Find(@new.AddedId);
|
|
|
|
|
|
|
|
|
|
if(machine is null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
switch(@new.Type)
|
|
|
|
|
{
|
|
|
|
|
case NewsType.NewComputerInDb:
|
2020-05-22 03:35:50 +01:00
|
|
|
news.Add(new NewsViewModel(@new.AddedId, _l["New computer in database"], @new.Date, "machine",
|
2020-05-21 17:23:46 +01:00
|
|
|
$"{machine.Company.Name} {machine.Name}"));
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case NewsType.NewConsoleInDb:
|
2020-05-22 03:35:50 +01:00
|
|
|
news.Add(new NewsViewModel(@new.AddedId, _l["New console in database"], @new.Date, "machine",
|
2020-05-21 17:23:46 +01:00
|
|
|
$"{machine.Company.Name} {machine.Name}"));
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case NewsType.NewComputerInCollection:
|
2020-05-22 03:35:50 +01:00
|
|
|
news.Add(new NewsViewModel(@new.AddedId, _l["New computer in collection"], @new.Date, "machine",
|
2020-05-21 17:23:46 +01:00
|
|
|
$"{machine.Company.Name} {machine.Name}"));
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case NewsType.NewConsoleInCollection:
|
2020-05-22 03:35:50 +01:00
|
|
|
news.Add(new NewsViewModel(@new.AddedId, _l["New console in collection"], @new.Date, "machine",
|
2020-05-21 17:23:46 +01:00
|
|
|
$"{machine.Company.Name} {machine.Name}"));
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case NewsType.UpdatedComputerInDb:
|
2020-05-21 18:52:40 +01:00
|
|
|
news.Add(new NewsViewModel(@new.AddedId, _l["Updated computer in database"], @new.Date,
|
2020-05-22 03:35:50 +01:00
|
|
|
"machine", $"{machine.Company.Name} {machine.Name}"));
|
2020-05-21 17:23:46 +01:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case NewsType.UpdatedConsoleInDb:
|
2020-05-21 18:52:40 +01:00
|
|
|
news.Add(new NewsViewModel(@new.AddedId, _l["Updated console in database"], @new.Date,
|
2020-05-22 03:35:50 +01:00
|
|
|
"machine", $"{machine.Company.Name} {machine.Name}"));
|
2020-05-21 17:23:46 +01:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case NewsType.UpdatedComputerInCollection:
|
2020-05-21 18:52:40 +01:00
|
|
|
news.Add(new NewsViewModel(@new.AddedId, _l["Updated computer in collection"], @new.Date,
|
2020-05-22 03:35:50 +01:00
|
|
|
"machine", $"{machine.Company.Name} {machine.Name}"));
|
2020-05-21 17:23:46 +01:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case NewsType.UpdatedConsoleInCollection:
|
2020-05-21 18:52:40 +01:00
|
|
|
news.Add(new NewsViewModel(@new.AddedId, _l["Updated console in collection"], @new.Date,
|
2020-05-22 03:35:50 +01:00
|
|
|
"machine", $"{machine.Company.Name} {machine.Name}"));
|
2020-05-21 17:23:46 +01:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case NewsType.NewMoneyDonation:
|
|
|
|
|
// TODO
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default: continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return news;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|