147 lines
5.3 KiB
C#
147 lines
5.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Data.Entity;
|
|
using System.Diagnostics.Contracts;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using MovieDB.Migrations;
|
|
|
|
namespace MovieDB
|
|
{
|
|
public class Movie
|
|
{
|
|
public int Id { get; set; }
|
|
[Required]
|
|
public string Name { get; set; }
|
|
[Required]
|
|
public int Year { get; set; }
|
|
public List<string> Zanrs { get; set; }
|
|
public virtual List<ActorRole> Actors { get; set; }
|
|
|
|
public Movie()
|
|
{
|
|
this.Zanrs = new List<string>();
|
|
this.Actors = new List<ActorRole>();
|
|
}
|
|
}
|
|
|
|
public class ActorRole
|
|
{
|
|
public int Id { get; set; }
|
|
[Required]
|
|
public string Role { get; set; }
|
|
[Required]
|
|
public Actor Actor { get; set; }
|
|
}
|
|
|
|
public class Actor
|
|
{
|
|
public int Id { get; set; }
|
|
[Required]
|
|
public string FirstName { get; set; }
|
|
[Required]
|
|
public string LastName { get; set; }
|
|
[Required]
|
|
public bool Active { get; set; }
|
|
[Required]
|
|
public int BirthYear { get; set; }
|
|
public virtual List<Movie> Movies { get; set; }
|
|
|
|
public Actor ()
|
|
{
|
|
this.Active = true;
|
|
this.Movies = new List<Movie>();
|
|
}
|
|
}
|
|
|
|
public class MovieDBContext : DbContext
|
|
{
|
|
public DbSet<Movie> Movies { get; set; }
|
|
public DbSet<ActorRole> ActorRoles { get; set; }
|
|
public DbSet<Actor> Actors { get; set; }
|
|
|
|
public MovieDBContext() : base("name=MovieDBBase")
|
|
{
|
|
Database.SetInitializer(
|
|
new MigrateDatabaseToLatestVersion<MovieDBContext, Configuration>());
|
|
}
|
|
}
|
|
|
|
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
using (MovieDBContext db = new MovieDBContext())
|
|
{
|
|
bool running = true;
|
|
while (running)
|
|
{
|
|
Console.Write("> ");
|
|
string[] input = Console.ReadLine().Split(' ');
|
|
switch (input[0])
|
|
{
|
|
case "exit":
|
|
running = false;
|
|
break;
|
|
case "listmovies":
|
|
foreach (var movie in db.Movies)
|
|
{
|
|
Console.WriteLine($"{movie.Id}: {movie.Name} ({movie.Year})");
|
|
}
|
|
Console.WriteLine($"Movie count: {db.Movies.Count()}");
|
|
break;
|
|
case "addmovie":
|
|
var m = new Movie();
|
|
m.Name = input[1];
|
|
m.Year = int.Parse(input[2]);
|
|
db.Movies.Add(m);
|
|
db.SaveChanges();
|
|
break;
|
|
|
|
case "addactor":
|
|
var a = new Actor();
|
|
a.FirstName = input[1];
|
|
a.LastName = input[2];
|
|
a.BirthYear = int.Parse(input[3]);
|
|
db.Actors.Add(a);
|
|
db.SaveChanges();
|
|
break;
|
|
case "listactors":
|
|
foreach (var actor in db.Actors)
|
|
{
|
|
Console.WriteLine($"{actor.Id}: {actor.FirstName} {actor.LastName} ({actor.BirthYear}), movie count: {actor.Movies.Count()}");
|
|
}
|
|
Console.WriteLine($"Actor count: {db.Actors.Count()}");
|
|
break;
|
|
|
|
case "addactormovie":
|
|
var ar = new ActorRole();
|
|
var am = db.Movies.Find(int.Parse(input[1]));
|
|
am.Actors.Add(ar);
|
|
ar.Actor = db.Actors.Find(int.Parse(input[2]));
|
|
ar.Role = input[3];
|
|
break;
|
|
|
|
case "listactormovie":
|
|
var mov = db.Movies.Find(int.Parse(input[1]));
|
|
foreach (var actor in mov.Actors)
|
|
{
|
|
Console.WriteLine($"{actor.Id}: {actor.Actor.FirstName} {actor.Actor.LastName} ({actor.Actor.BirthYear}) {actor.Role}");
|
|
}
|
|
Console.WriteLine($"Number of actors for movie {mov.Name}: {mov.Actors.Count}");
|
|
break;
|
|
case "help":
|
|
Console.WriteLine("listmovies: Lists all movies\naddmovie <name> <year>: Adds a movie\nlistactors: List all actors\naddactor <FirstName> <LastName> <BirthYear>: Add a new actor\nlistactormovie <MovieId>: List actors for a movie\naddactormovie <MovieId> <ActorId> <Role>: Add a actor to a movie with a role");
|
|
break;
|
|
default:
|
|
Console.WriteLine("No such command. Try help.");
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |