Advertisement
Guest User

Code a la Wishy

a guest
May 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.69 KB | None | 0 0
  1. private String queryString = "select * from RisoeOpgave";
  2. private String queryStringFromID = "select * from RisoeOpgave where ID = @ID";
  3. private String insertSql = "insert into RisoeOpgave Values (@OpgaveID, @Beskrivelse, @Status, @Ventetid, @UdstyrID)";
  4. private String deleteSql = "delete from RisoeOpgave where ID = @ID";
  5. private String updateSql = "update RisoeOpgave " +
  6. "set ID = @OpgaveID, Beskrivelse = @Beskrivelse, Status = @Status, VentetidIDage = @Ventetid " +
  7. "where ID = @ID";
  8.  
  9. public List<Opgave> HentAlleOpgaver()
  10. {
  11. List<Opgave> opgaver = new List<Opgave>();
  12.  
  13. using (SqlConnection connection = new SqlConnection(connectionString))
  14. {
  15. SqlCommand command = new SqlCommand(queryString, connection);
  16. command.Connection.Open();
  17.  
  18. SqlDataReader reader = command.ExecuteReader();
  19. while (reader.Read())
  20. {
  21. opgaver.Add(ReadOpgave(reader));
  22. }
  23. }
  24. return opgaver;
  25. }
  26.  
  27. public Opgave HentOpgaveFraId(int opgaveId)
  28. {
  29. using (SqlConnection connection = new SqlConnection(connectionString))
  30. {
  31. SqlCommand command = new SqlCommand(queryStringFromID, connection);
  32. command.Parameters.AddWithValue("@ID", opgaveId);
  33.  
  34. command.Connection.Open();
  35.  
  36. SqlDataReader reader = command.ExecuteReader();
  37. if (reader.Read())
  38. {
  39. return ReadOpgave(reader);
  40. }
  41. }
  42. return null;
  43. }
  44.  
  45. public bool IndsætOpgave(Opgave opgave)
  46. {
  47. using (SqlConnection connection = new SqlConnection(connectionString))
  48. {
  49. SqlCommand command = new SqlCommand(insertSql, connection);
  50.  
  51. TilføjVærdiOpgave(opgave, command);
  52.  
  53. command.Connection.Open();
  54.  
  55. int noOfRows = command.ExecuteNonQuery();
  56.  
  57. if (noOfRows == 1)
  58. {
  59. return true;
  60. }
  61. return false;
  62. }
  63. }
  64.  
  65. public bool OpdaterOpgave(Opgave opgave, int opgaveID)
  66. {
  67. using (SqlConnection connection = new SqlConnection(connectionString))
  68. {
  69. SqlCommand command = new SqlCommand(updateSql, connection);
  70.  
  71. TilføjVærdiOpgave(opgave, command);
  72. command.Parameters.AddWithValue("@ID", opgaveID);
  73.  
  74. command.Connection.Open();
  75.  
  76. int noOfRows = command.ExecuteNonQuery();
  77.  
  78. if (noOfRows == 1)
  79. {
  80. return true;
  81. }
  82. return false;
  83. }
  84. }
  85.  
  86. public Opgave SletOpgave(int opgaveID)
  87. {
  88. Opgave opgave = HentOpgaveFraId(opgaveID);
  89. if (opgave == null)
  90. {
  91. return null;
  92. }
  93.  
  94. using (SqlConnection connection = new SqlConnection(connectionString))
  95. {
  96. SqlCommand command = new SqlCommand(deleteSql, connection);
  97. command.Parameters.AddWithValue("@ID", opgaveID);
  98.  
  99. command.Connection.Open();
  100.  
  101. int noOfRows = command.ExecuteNonQuery();
  102.  
  103. if (noOfRows == 1)
  104. {
  105. return opgave;
  106. }
  107. return null;
  108. }
  109. }
  110.  
  111. private void CheckEnumParseO(StatusType checkStatus, int checkId)
  112. {
  113. if (!(checkStatus == StatusType.Fejlet ||
  114. checkStatus == StatusType.IkkeLøst ||
  115. checkStatus == StatusType.Løst))
  116. {
  117. int exId = checkId;
  118. throw new ParseToEnumException(exId);
  119. }
  120. }
  121.  
  122. //HentAlle og HentFraID (DRY)
  123. private Opgave ReadOpgave(SqlDataReader reader)
  124. {
  125. int id = reader.GetInt32(0);
  126. String beskrivelse = reader.GetString(1);
  127.  
  128. StatusType status = StatusType.IkkeLøst;
  129. try
  130. {
  131. String statusStr = reader.GetString(2);
  132. status = (StatusType)Enum.Parse(typeof(StatusType), statusStr);
  133. CheckEnumParseO(status, id);
  134. }
  135. catch (ParseToEnumException)
  136. {
  137. ParseToEnumException parseFailEx = new ParseToEnumException(id);
  138. string log = parseFailEx.ToString(); //string til log for exceptions på REST Siden. ikke lagret endnu. mangler liste til at blive lagret i.
  139.  
  140. }
  141.  
  142. int ventetid = reader.GetInt32(3);
  143. int udstyrId = reader.GetInt32(4);
  144.  
  145. Udstyr udstyr = HentUdstyrFraId(udstyrId);
  146.  
  147. return new Opgave(id, beskrivelse, status, udstyrId, ventetid, udstyr); //hvad der der galt med opgave konstructor?
  148. }
  149.  
  150. //Indsæt og Opdater (DRY)
  151. private void TilføjVærdiOpgave(Opgave opgave, SqlCommand command)
  152. {
  153. command.Parameters.AddWithValue("@OpgaveID", opgave.ID);
  154. command.Parameters.AddWithValue("@Beskrivelse", opgave.Beskrivelse);
  155. command.Parameters.AddWithValue("@Status", opgave.Status.ToString());
  156. command.Parameters.AddWithValue("@Ventetid", opgave.VentetidIDage);
  157. command.Parameters.AddWithValue("@UdstyrID", opgave.Udstyr.UdstyrId);
  158. }
  159.  
  160. //Extra hjælp for at hente udstyr
  161. private String queryStringFromIDUdstyr = "select * from RisoeUdstyr where UdstyrId = @UdstyrId";
  162.  
  163. private String queryStringFromID2 = "select * from RisoeStation where StationNr = @StationNr";
  164.  
  165. public Udstyr HentUdstyrFraId(int id)
  166. {
  167. using (SqlConnection connection = new SqlConnection(connectionString))
  168. {
  169. SqlCommand command = new SqlCommand(queryStringFromIDUdstyr, connection);
  170. command.Parameters.AddWithValue("@UdstyrId", id);
  171.  
  172. command.Connection.Open();
  173.  
  174. SqlDataReader reader = command.ExecuteReader();
  175. if (reader.Read())
  176. {
  177. return ReadUdstyr(reader);
  178. }
  179. }
  180. return null; //Kan vi skrive dette?
  181. }
  182.  
  183. private Udstyr ReadUdstyr(SqlDataReader reader) //denne metode skal justeres så den tager fat de rigtige steder i DB
  184. {
  185. int udstyrId = reader.GetInt32(0);
  186. int stationId = reader.GetInt32(1);
  187.  
  188. Station station = HentStationFraId(stationId);
  189.  
  190. uType type = uType.Filter;
  191. try
  192. {
  193. string typeStr = reader.GetString(2);
  194. type = (uType)Enum.Parse(typeof(uType), typeStr);
  195.  
  196. CheckEnumParseU(type, udstyrId);
  197. }
  198. catch (ParseToEnumException)
  199. {
  200. ParseToEnumException parseFailEx = new ParseToEnumException(udstyrId);
  201. string log = parseFailEx.ToString();
  202.  
  203. }
  204.  
  205. DateTime instDato = reader.GetDateTime(3);
  206. string beskrivelse = reader.GetString(4);
  207.  
  208. return new Udstyr(udstyrId, instDato, beskrivelse, type, station);
  209. }
  210.  
  211. private void CheckEnumParseU(uType checkType, int checkId)
  212. {
  213. if (!(checkType == uType.Filter ||
  214. checkType == uType.Termometer ||
  215. checkType == uType.Lufttrykmåler))
  216. {
  217. int exId = checkId;
  218. throw new ParseToEnumException(exId);
  219. }
  220. }
  221.  
  222. public Station HentStationFraId(int id)
  223. {
  224. using (SqlConnection connection = new SqlConnection(connectionString))
  225. {
  226. SqlCommand command = new SqlCommand(queryStringFromID2, connection);
  227. command.Parameters.AddWithValue("@StationNr", id);
  228.  
  229. command.Connection.Open();
  230.  
  231. SqlDataReader reader = command.ExecuteReader();
  232. if (reader.Read())
  233. {
  234. return ReadStation(reader);
  235. }
  236. }
  237. return null; //Kan vi skrive dette?
  238. }
  239.  
  240. private Station ReadStation(SqlDataReader reader)
  241. {
  242. int stationNr = reader.GetInt32(0);
  243. String navn = reader.GetString(1);
  244.  
  245. return new Station(navn, stationNr);
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement