Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. using VideoGalore.Models.Dtos;
  7. using VideoGalore.Models.Entities;
  8. using Microsoft.AspNetCore.Hosting;
  9. using VideoGalore.Repositories.Data;
  10.  
  11.  
  12. namespace VideoGalore.Repositories.Data
  13. {
  14. public class TapeData
  15. {
  16. private List<Tape> tapes;
  17. private IDictionary<int, Tape> tapeId;
  18.  
  19.  
  20. public TapeData() {
  21.  
  22. //tapeId = new Dictionary<int, Tape>();
  23. //tapes = readTapeJson();
  24. }
  25.  
  26. public void init() {
  27. tapeId = new Dictionary<int, Tape>();
  28. tapes = readTapeJson();
  29. }
  30.  
  31. public void deleteTape(int id){
  32. //tapeId.Remove(id);
  33. int index = 0;
  34. for(int i = 0; i < tapes.Count; i++){
  35. if (tapes[i].Id == id){
  36. index = i;
  37. }
  38. }
  39. tapes.RemoveAt(index);
  40.  
  41. }
  42.  
  43. public TapeDto getTapeById(int id) {
  44. if(tapeId.ContainsKey(id)){
  45. TapeDto newTape = new TapeDto();
  46. newTape.Id = tapeId[id].Id;
  47. newTape.name = tapeId[id].title;
  48.  
  49. return newTape;
  50. }
  51. else {
  52. return null;
  53. }
  54. }
  55.  
  56. //public void createTape(Tape tape){
  57. // tapes.Add(tape);
  58. //}
  59. public bool createTape(Tape tape){
  60. tapes.Add(tape);
  61. tapeId.Add(tape.Id, tape);
  62. foreach(var x in tapes){
  63. Console.WriteLine(x.title);
  64. }
  65. return addedCorrectly(tape);
  66. }
  67.  
  68. public bool addedCorrectly(Tape tape){
  69. return tapes.Contains(tape);
  70. }
  71.  
  72.  
  73.  
  74. private List<Tape> readTapeJson() {
  75.  
  76. List<Tape> items = new List<Tape>();
  77.  
  78. try {
  79. using(StreamReader sr = new StreamReader("..\\VideoGalore.Repositories\\Data\\Videotapes.json")){
  80. string json = sr.ReadToEnd();
  81.  
  82. JArray tapes = JArray.Parse(json);
  83. foreach(var tape in tapes) {
  84. int id = (int)tape["id"];
  85. String title = (String)tape["title"];
  86. String director = (String)tape["director_first_name"];
  87. String director_last_name = (String)tape["director_last_name"];
  88. String type = (String)tape["type"];
  89. String eidr = (String)tape["eidr"];
  90. DateTime releaseDate = (DateTime)tape["release_date"];
  91.  
  92. Tape newTape = new Tape();
  93. newTape.Id = id;
  94. newTape.director_name = director;
  95. newTape.director_last_name = director_last_name;
  96. newTape.eidr = eidr;
  97. newTape.release_date = releaseDate;
  98. newTape.title = title;
  99. newTape.type = type;
  100.  
  101. items.Add(newTape);
  102. tapeId.Add(id, newTape);
  103.  
  104. }
  105.  
  106. }
  107. }
  108. catch(Exception e) {
  109. Console.WriteLine("The file could not be read:");
  110. Console.WriteLine(e.Message);
  111. }
  112. return items;
  113. }
  114.  
  115. public bool updateTape(int id, Tape tape){
  116. foreach(var listTape in tapes){
  117. if(listTape.Id == tape.Id){
  118. listTape.release_date = tape.release_date;
  119. listTape.title = tape.title;
  120. listTape.director_name = tape.director_name;
  121. listTape.director_last_name = tape.director_last_name;
  122. listTape.eidr = tape.eidr;
  123. listTape.type = tape.type;
  124. if(tapes.Contains(tape)){
  125. return true;
  126. }
  127. else {
  128. return false;
  129. }
  130.  
  131. }
  132. }
  133. return false;
  134. }
  135.  
  136. public List<Tape> getAllTapes() {
  137. List<TapeDto> temp = new List<TapeDto>();
  138. foreach(var tape in tapes) {
  139. TapeDto newTape = new TapeDto();
  140. newTape.Id = tape.Id;
  141. newTape.name = tape.title;
  142. Console.WriteLine(newTape.name);
  143. temp.Add(newTape);
  144. }
  145. return tapes;
  146. }
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement