2018-04-13 17:53:12 +01:00
|
|
|
|
/******************************************************************************
|
|
|
|
|
|
// Canary Islands Computer Museum Website
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : News.cs
|
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Website news model
|
|
|
|
|
|
//
|
|
|
|
|
|
// --[ 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-2018 Natalia Portillo
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2018-04-13 02:42:27 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using Cicm.Database.Schemas;
|
|
|
|
|
|
|
|
|
|
|
|
namespace cicm_web.Models
|
|
|
|
|
|
{
|
|
|
|
|
|
public class News
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>Affected ID</summary>
|
|
|
|
|
|
public int AffectedId;
|
|
|
|
|
|
/// <summary>Date</summary>
|
|
|
|
|
|
public DateTime Date;
|
|
|
|
|
|
/// <summary>URL of image</summary>
|
|
|
|
|
|
public string Image;
|
2018-04-14 03:04:52 +01:00
|
|
|
|
/// <summary>Subtext</summary>
|
|
|
|
|
|
public string SubText;
|
2018-04-13 02:42:27 +01:00
|
|
|
|
/// <summary>URL of target view, if applicable</summary>
|
|
|
|
|
|
public string TargetView;
|
|
|
|
|
|
/// <summary>Text</summary>
|
|
|
|
|
|
public string Text;
|
|
|
|
|
|
|
|
|
|
|
|
public static News[] GetAllItems()
|
|
|
|
|
|
{
|
|
|
|
|
|
List<Cicm.Database.Schemas.News> dbItems = null;
|
|
|
|
|
|
bool? result = Program.Database?.Operations.GetNews(out dbItems);
|
|
|
|
|
|
if(result == null || result.Value == false || dbItems == null) return null;
|
|
|
|
|
|
|
|
|
|
|
|
return dbItems.OrderByDescending(i => i.Id).Select(TransformItem) as News[];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static News[] GetLastItems(int count = 10)
|
|
|
|
|
|
{
|
|
|
|
|
|
List<Cicm.Database.Schemas.News> dbItems = null;
|
|
|
|
|
|
bool? result = Program.Database?.Operations.GetNews(out dbItems);
|
|
|
|
|
|
if(result == null || result.Value == false || dbItems == null) return null;
|
|
|
|
|
|
|
|
|
|
|
|
return dbItems.OrderByDescending(i => i.Id).Take(count).Select(TransformItem).ToArray();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static News TransformItem(Cicm.Database.Schemas.News dbItem)
|
|
|
|
|
|
{
|
2018-04-14 03:04:52 +01:00
|
|
|
|
string imageUrl;
|
|
|
|
|
|
string text;
|
|
|
|
|
|
string targetView;
|
|
|
|
|
|
string subtext;
|
|
|
|
|
|
Computer computer;
|
|
|
|
|
|
OwnComputer owncomputer;
|
|
|
|
|
|
Console console;
|
|
|
|
|
|
OwnConsole ownconsole;
|
2018-04-13 02:42:27 +01:00
|
|
|
|
|
|
|
|
|
|
switch(dbItem.Type)
|
|
|
|
|
|
{
|
|
|
|
|
|
case NewsType.NewComputerInDb:
|
|
|
|
|
|
text = "New computer added to the database.";
|
|
|
|
|
|
imageUrl = "assets/photos/computers/";
|
2018-04-14 04:41:04 +01:00
|
|
|
|
targetView = "Computer";
|
2018-04-14 03:04:52 +01:00
|
|
|
|
computer = Computer.GetItem(dbItem.AffectedId);
|
|
|
|
|
|
subtext = $"{computer.Company.Name} - {computer.Model}";
|
2018-04-13 02:42:27 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case NewsType.NewConsoleInDb:
|
|
|
|
|
|
text = "New videoconsole added to the database.";
|
|
|
|
|
|
imageUrl = "assets/photos/consoles/";
|
|
|
|
|
|
targetView = "console";
|
2018-04-14 03:04:52 +01:00
|
|
|
|
console = Console.GetItem(dbItem.AffectedId);
|
|
|
|
|
|
subtext = $"{console.Company.Name} - {console.Name}";
|
2018-04-13 02:42:27 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case NewsType.NewComputerInCollection:
|
2018-04-14 03:04:52 +01:00
|
|
|
|
text = "New computer added to the museum's collection.";
|
|
|
|
|
|
imageUrl = "assets/photos/computers/";
|
|
|
|
|
|
targetView = "collection_computer";
|
|
|
|
|
|
owncomputer = OwnComputer.GetItem(dbItem.AffectedId);
|
|
|
|
|
|
subtext = $"{owncomputer.Computer.Company.Name} - {owncomputer.Computer.Model}";
|
2018-04-13 02:42:27 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case NewsType.NewConsoleInCollection:
|
|
|
|
|
|
text = "New videoconsole added to the museum's collection.";
|
|
|
|
|
|
imageUrl = "assets/photos/consoles/";
|
|
|
|
|
|
targetView = "collection_console";
|
2018-04-14 03:04:52 +01:00
|
|
|
|
ownconsole = OwnConsole.GetItem(dbItem.AffectedId);
|
|
|
|
|
|
subtext = $"{ownconsole.Console.Company.Name} - {ownconsole.Console.Name}";
|
2018-04-13 02:42:27 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case NewsType.UpdatedComputerInDb:
|
|
|
|
|
|
text = "Updated computer from the database.";
|
|
|
|
|
|
imageUrl = "assets/photos/computers/";
|
|
|
|
|
|
targetView = "computer";
|
2018-04-14 03:04:52 +01:00
|
|
|
|
computer = Computer.GetItem(dbItem.AffectedId);
|
|
|
|
|
|
subtext = $"{computer.Company.Name} - {computer.Model}";
|
2018-04-13 02:42:27 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case NewsType.UpdatedConsoleInDb:
|
|
|
|
|
|
text = "Updated videoconsole from the database.";
|
|
|
|
|
|
imageUrl = "assets/photos/consoles/";
|
|
|
|
|
|
targetView = "console";
|
2018-04-14 03:04:52 +01:00
|
|
|
|
console = Console.GetItem(dbItem.AffectedId);
|
|
|
|
|
|
subtext = $"{console.Company.Name} - {console.Name}";
|
2018-04-13 02:42:27 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case NewsType.UpdatedComputerInCollection:
|
2018-04-14 03:04:52 +01:00
|
|
|
|
text = "Updated computer from museum's collection.";
|
|
|
|
|
|
imageUrl = "assets/photos/computers/";
|
|
|
|
|
|
targetView = "collection_computer";
|
|
|
|
|
|
owncomputer = OwnComputer.GetItem(dbItem.AffectedId);
|
|
|
|
|
|
subtext = $"{owncomputer.Computer.Company.Name} - {owncomputer.Computer.Model}";
|
2018-04-13 02:42:27 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case NewsType.UpdatedConsoleInCollection:
|
|
|
|
|
|
text = "Updated videoconsole from museum's collection.";
|
|
|
|
|
|
imageUrl = "assets/photos/consoles/";
|
|
|
|
|
|
targetView = "collection_console";
|
2018-04-14 03:04:52 +01:00
|
|
|
|
ownconsole = OwnConsole.GetItem(dbItem.AffectedId);
|
|
|
|
|
|
subtext = $"{ownconsole.Console.Company.Name} - {ownconsole.Console.Name}";
|
2018-04-13 02:42:27 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case NewsType.NewMoneyDonation:
|
|
|
|
|
|
text = "New money donation.";
|
|
|
|
|
|
imageUrl = null;
|
|
|
|
|
|
targetView = null;
|
2018-04-14 03:04:52 +01:00
|
|
|
|
subtext = null;
|
2018-04-13 02:42:27 +01:00
|
|
|
|
break;
|
|
|
|
|
|
default: throw new ArgumentOutOfRangeException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new News
|
|
|
|
|
|
{
|
|
|
|
|
|
AffectedId = dbItem.AffectedId,
|
|
|
|
|
|
Date = DateTime.ParseExact(dbItem.Date, "yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture),
|
|
|
|
|
|
Image = imageUrl == null ? null : imageUrl + $"{dbItem.AffectedId}",
|
|
|
|
|
|
Text = text,
|
2018-04-14 03:04:52 +01:00
|
|
|
|
TargetView = targetView,
|
|
|
|
|
|
SubText = subtext
|
2018-04-13 02:42:27 +01:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|