PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/ITELCA_CLASSLIBRARY/Services/ServicioTestimonio.cs

https://gitlab.com/oscarsalazarsevilla/ITELCA
C# | 194 lines | 179 code | 15 blank | 0 comment | 17 complexity | 9c76e7a656e52da4c82898317dbae494 MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. using System.Data.Entity;
  6. using ITELCA_CLASSLIBRARY.Models;
  7. using ITELCA_CLASSLIBRARY.Repositories;
  8. using ITELCA_CLASSLIBRARY.CustomClasses;
  9. using System.Collections;
  10. using System.Web;
  11. using UmbrellaClassLibrary.CustomClasses;
  12. namespace ITELCA_CLASSLIBRARY.Services
  13. {
  14. public class ServicioTestimonio
  15. {
  16. private UnitOfWork unidad;
  17. public ServicioTestimonio()
  18. {
  19. this.unidad = new UnitOfWork();
  20. }
  21. public bool Guardar(TESTIMONIOS testimonio)
  22. {
  23. try
  24. {
  25. testimonio.CONDICION = "C";
  26. unidad.RepositorioTestimonio.Add(testimonio);
  27. unidad.Save();
  28. return true;
  29. }
  30. catch
  31. {
  32. return false;
  33. }
  34. }
  35. public bool Modificar(TESTIMONIOS entity)
  36. {
  37. try
  38. {
  39. entity.CONDICION = "A";
  40. unidad.RepositorioTestimonio.Modify(entity);
  41. unidad.Save();
  42. return true;
  43. }
  44. catch
  45. {
  46. return false;
  47. }
  48. }
  49. public List<object> Eliminar(decimal testimonio)
  50. {
  51. List<object> lista = new List<object>();
  52. try
  53. {
  54. var aux = unidad.RepositorioTestimonio.GetById((int)testimonio);
  55. if (aux != null)
  56. {
  57. lista.Add(new
  58. {
  59. ok = 1,
  60. msg = "Testimonio Eliminado"
  61. });
  62. aux.ESTADO = 0;
  63. aux.CONDICION = "E";
  64. unidad.RepositorioTestimonio.Modify(aux);
  65. unidad.Save();
  66. return lista;
  67. }
  68. else
  69. {
  70. lista.Add(new
  71. {
  72. ok = 0,
  73. msg = "Testimonio No encontrado"
  74. });
  75. return lista;
  76. }
  77. }
  78. catch
  79. {
  80. lista.Clear();
  81. lista.Add(new
  82. {
  83. ok = 0,
  84. msg = "Error Eliminando Testimonio"
  85. });
  86. return lista;
  87. }
  88. }
  89. public object ObtenerDataGrid(string sidx, string sord, int page, int rows, string filters)
  90. {
  91. int totalPages = 0;
  92. int totalRecords = 0;
  93. IEnumerable<TESTIMONIOS> testimonios;
  94. testimonios = unidad.RepositorioTestimonio.GetAll().Where(u => u.ESTADO == 1 || (u.ESTADO == 0 && u.CONDICION != "E"));
  95. testimonios = JqGrid<TESTIMONIOS>.GetFilteredContent(sidx, sord, page, rows, filters, testimonios.AsQueryable(), ref totalPages, ref totalRecords);
  96. var rowsModel = (
  97. from testimonio in testimonios.ToList()
  98. select new
  99. {
  100. i = testimonio.TESTIMONIO_ID,
  101. cell = new string[] {
  102. testimonio.TESTIMONIO_ID.ToString(),
  103. testimonio.TEXTO.ToString(),
  104. testimonio.NOMBRE.ToString(),
  105. testimonio.ESTADO.ToString(),
  106. "<a title=\"Editar\" href=\"/Testimonios/Editar/"+ testimonio.TESTIMONIO_ID+"\"><span id=\""+ testimonio.TESTIMONIO_ID+"\" class=\"ui-icon ui-icon-pencil\"></a>",
  107. "<span id=\""+ testimonio.TESTIMONIO_ID +"\" class=\"ui-icon ui-icon-close\" ></span>" }
  108. }).ToArray();
  109. return JqGrid<TESTIMONIOS>.SetJsonData(totalPages, totalRecords, page, rowsModel);
  110. }
  111. public TESTIMONIOS ObtenerPorClave(int id)
  112. {
  113. try
  114. {
  115. return unidad.RepositorioTestimonio.GetById(id);
  116. }
  117. catch
  118. {
  119. return null;
  120. }
  121. }
  122. public TESTIMONIOS ObtenerPorNombre(string nombre)
  123. {
  124. try
  125. {
  126. TESTIMONIOS testimonio = unidad.RepositorioTestimonio.GetAll().Where(u => u.NOMBRE.ToLower() == nombre.ToLower() && u.ESTADO==1).FirstOrDefault();
  127. if (testimonio != null)
  128. return testimonio;
  129. else
  130. return null;
  131. }
  132. catch
  133. {
  134. return null;
  135. }
  136. }
  137. public IEnumerable<TESTIMONIOS> ObtenerTodos()
  138. {
  139. try
  140. {
  141. IEnumerable<TESTIMONIOS> testimonios = unidad.RepositorioTestimonio.GetAll().Where(u=>u.ESTADO==1);
  142. if (testimonios == null)
  143. return Enumerable.Empty<TESTIMONIOS>();
  144. else
  145. return testimonios;
  146. }
  147. catch
  148. {
  149. return Enumerable.Empty<TESTIMONIOS>();
  150. }
  151. }
  152. public List<TESTIMONIOS> ObtenerOrdenados()
  153. {
  154. try
  155. {
  156. return ObtenerTodos().OrderByDescending(t=>t.FECHA_CREACION).ToList();
  157. }
  158. catch
  159. {
  160. return new List<TESTIMONIOS>();
  161. }
  162. }
  163. public bool Duplicado(TESTIMONIOS testimonio)
  164. {
  165. try
  166. {
  167. if (unidad.RepositorioTestimonio.GetAll().Where(u => u.NOMBRE.ToLower() == testimonio.NOMBRE.ToLower() &&
  168. u.TESTIMONIO_ID != testimonio.TESTIMONIO_ID && u.ESTADO==1).Count() > 0)
  169. return true;
  170. else
  171. return false;
  172. }
  173. catch
  174. {
  175. return false;
  176. }
  177. }
  178. }
  179. }