Skip to content

Instantly share code, notes, and snippets.

@boserup
Created November 8, 2017 09:43
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save boserup/dbabeb924bff2d3b5c78e9d6baeb669d to your computer and use it in GitHub Desktop.
/*
*Contains data about the concept of a book.
*
*/
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using System.ComponentModel.DataAnnotations;
using CentralServer.ObjectMapper;
using CentralServer.Utilities;
using System.Linq;
namespace CentralServer.Models.MediaModels {
public class Book : Media, IMedia<Book>, ISerializeable, IConvertable<Book> {
[Required]
[StringLength(20)]
public String ISBN {get; set;}
[StringLength(69)]
public String Author {get; set;}
[DataType(DataType.Date)]
public DateTime ReleaseDate {get; set;}
[StringLength(64)]
public String Publisher {get; set;}
public int PageCount {get; set;}
public Book() {
}
public Book(String id, String name, List<Library> location, bool available, List<Category> categories,
double price, int amount, String shortDesc, String fullDesc, Language lang,
String isbn, String author, DateTime releaseDate, String publisher, int pageCount) {
this.ID = id;
this.Name = name;
this.Location = location;
this.Available = available;
this.Categories = categories;
this.Price = price;
this.ShortDesc = shortDesc;
this.FullDesc = fullDesc;
this.Lang = (Language)lang;
this.ISBN = isbn;
this.Author = author;
this.ReleaseDate = releaseDate;
this.Publisher = publisher;
this.PageCount = pageCount;
}
public Book(String name, List<Library> location, bool available, List<Category> categories,
double price, int amount, String shortDesc, String fullDesc, Language lang,
String isbn, String author, DateTime releaseDate, String publisher, int pageCount) {
this.Name = name;
this.Location = location;
this.Available = available;
this.Categories = categories;
this.Price = price;
this.ShortDesc = shortDesc;
this.FullDesc = fullDesc;
this.Lang = lang;
this.ISBN = isbn;
this.Author = author;
this.ReleaseDate = releaseDate;
this.Publisher = publisher;
this.PageCount = pageCount;
}
public Book CreateFromID(String id) {
return null;
}
public new string GetTableName() {
return "books";
}
public new string GetPrimaryKey() {
return "id";
}
public new Dictionary<string, object> GetValues() {
Dictionary<string, object> values = this.ToDictionary();
values.Remove("Location");
values.Remove("Categories");
// Remove price on CentralServer, since local libraries decide prices them selves
values.Remove("Price");
// Remove primary key, since we can reference it later from the
// GetPrimaryKey method.
values.Remove("ID");
// Language
values.Add("language", values["Lang"]);
values.Remove("Lang");
return values;
}
public new bool Create() {
return PersistentObject.INSTANCE.Save(this);
}
public new bool Delete() {
return PersistentObject.INSTANCE.Delete(this, this.ID);
}
public new bool Modify() {
return PersistentObject.INSTANCE.Modify(this, this.ID);
}
public new Book CreateFromDictionary(Dictionary<string, object> dictionary) {
return new Book(
dictionary["id"].ToString(),
dictionary["name"].ToString(),
new List<Library>(),
(bool)dictionary["available"],
new List<Category>(),
0.0,
Convert.ToInt32(dictionary["amount"]),
dictionary["shortdesc"].ToString(),
dictionary["fulldesc"].ToString(),
(Language)dictionary["language"].ToString(),
dictionary["isbn"].ToString(),
dictionary["author"].ToString(),
DateTime.Parse(dictionary["releasedate"].ToString()),
dictionary["publisher"].ToString(),
Convert.ToInt32(dictionary["pagecount"])
);
}
public static List<Book> All()
{
return PersistentObject.INSTANCE.Fetch(
new Dictionary<string, object>(),
"books",
typeof(Book)
).ConvertAll(x => (Book)x);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment