Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using VideoGalore.Models.Dtos;
- using VideoGalore.Models.Entities;
- using Microsoft.AspNetCore.Hosting;
- using VideoGalore.Repositories.Data;
- namespace VideoGalore.Repositories.Data
- {
- public class TapeData
- {
- private List<Tape> tapes;
- private IDictionary<int, Tape> tapeId;
- public TapeData() {
- //tapeId = new Dictionary<int, Tape>();
- //tapes = readTapeJson();
- }
- public void init() {
- tapeId = new Dictionary<int, Tape>();
- tapes = readTapeJson();
- }
- public void deleteTape(int id){
- //tapeId.Remove(id);
- int index = 0;
- for(int i = 0; i < tapes.Count; i++){
- if (tapes[i].Id == id){
- index = i;
- }
- }
- tapes.RemoveAt(index);
- }
- public TapeDto getTapeById(int id) {
- if(tapeId.ContainsKey(id)){
- TapeDto newTape = new TapeDto();
- newTape.Id = tapeId[id].Id;
- newTape.name = tapeId[id].title;
- return newTape;
- }
- else {
- return null;
- }
- }
- //public void createTape(Tape tape){
- // tapes.Add(tape);
- //}
- public bool createTape(Tape tape){
- tapes.Add(tape);
- tapeId.Add(tape.Id, tape);
- foreach(var x in tapes){
- Console.WriteLine(x.title);
- }
- return addedCorrectly(tape);
- }
- public bool addedCorrectly(Tape tape){
- return tapes.Contains(tape);
- }
- private List<Tape> readTapeJson() {
- List<Tape> items = new List<Tape>();
- try {
- using(StreamReader sr = new StreamReader("..\\VideoGalore.Repositories\\Data\\Videotapes.json")){
- string json = sr.ReadToEnd();
- JArray tapes = JArray.Parse(json);
- foreach(var tape in tapes) {
- int id = (int)tape["id"];
- String title = (String)tape["title"];
- String director = (String)tape["director_first_name"];
- String director_last_name = (String)tape["director_last_name"];
- String type = (String)tape["type"];
- String eidr = (String)tape["eidr"];
- DateTime releaseDate = (DateTime)tape["release_date"];
- Tape newTape = new Tape();
- newTape.Id = id;
- newTape.director_name = director;
- newTape.director_last_name = director_last_name;
- newTape.eidr = eidr;
- newTape.release_date = releaseDate;
- newTape.title = title;
- newTape.type = type;
- items.Add(newTape);
- tapeId.Add(id, newTape);
- }
- }
- }
- catch(Exception e) {
- Console.WriteLine("The file could not be read:");
- Console.WriteLine(e.Message);
- }
- return items;
- }
- public bool updateTape(int id, Tape tape){
- foreach(var listTape in tapes){
- if(listTape.Id == tape.Id){
- listTape.release_date = tape.release_date;
- listTape.title = tape.title;
- listTape.director_name = tape.director_name;
- listTape.director_last_name = tape.director_last_name;
- listTape.eidr = tape.eidr;
- listTape.type = tape.type;
- if(tapes.Contains(tape)){
- return true;
- }
- else {
- return false;
- }
- }
- }
- return false;
- }
- public List<Tape> getAllTapes() {
- List<TapeDto> temp = new List<TapeDto>();
- foreach(var tape in tapes) {
- TapeDto newTape = new TapeDto();
- newTape.Id = tape.Id;
- newTape.name = tape.title;
- Console.WriteLine(newTape.name);
- temp.Add(newTape);
- }
- return tapes;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement