PageRenderTime 60ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/website/control/TeacherControl.ashx.cs

https://github.com/panmingzhi815/SchoolManagementSystem
C# | 195 lines | 174 code | 18 blank | 3 comment | 16 complexity | 9080ec4227984148d4259eee77b7b14f MD5 | raw file
Possible License(s): LGPL-2.1
  1. using System;
  2. using System.Collections;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Services;
  7. using System.Web.Services.Protocols;
  8. using System.Xml.Linq;
  9. using DataService.service.dao;
  10. using Domain.Entities;
  11. using Newtonsoft.Json;
  12. using System.Reflection;
  13. namespace Domain.control
  14. {
  15. /// <summary>
  16. /// Summary description for $codebehindclassname$
  17. /// </summary>
  18. [WebService(Namespace = "http://tempuri.org/")]
  19. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  20. public class TeacherControl : IHttpHandler
  21. {
  22. public HttpContext context;
  23. public void ProcessRequest(HttpContext context)
  24. {
  25. this.context = context;
  26. context.Response.ContentType = "text/plain";
  27. String method = context.Request.Form.Get("method");
  28. if (method == null)
  29. {
  30. method = context.Request.QueryString["method"];
  31. }
  32. switch (method)
  33. {
  34. case "addTeacher":
  35. addTeacher();
  36. break;
  37. case "delTeacher":
  38. delTeacher();
  39. break;
  40. case "updateTeacher":
  41. updateTeacher();
  42. break;
  43. case "getTeachers":
  44. getTeachers();
  45. break;
  46. case "getTeacher":
  47. getTeacher();
  48. break;
  49. default:
  50. context.Response.Write("-1");
  51. break;
  52. }
  53. }
  54. public bool IsReusable
  55. {
  56. get
  57. {
  58. return false;
  59. }
  60. }
  61. public void addTeacher()
  62. {
  63. try
  64. {
  65. Teacher Teacher = new Teacher();
  66. setValue(Teacher, context);
  67. HttpPostedFile hpf = context.Request.Files["headImgFile"];
  68. if (hpf != null)
  69. {
  70. string serverPath = "/uploadFile/headImg/" + System.DateTime.Now.Ticks + "." + hpf.FileName.Split('.')[1];
  71. string savePath = context.Server.MapPath(serverPath);//路径,相对于服务器当前的路径
  72. hpf.SaveAs(savePath);//保存
  73. Teacher.HeadImage = serverPath;
  74. }
  75. TeacherService s = new TeacherService();
  76. s.save(Teacher);
  77. context.Response.Write("1");
  78. }
  79. catch (Exception e)
  80. {
  81. context.Response.Write("0");
  82. }
  83. }
  84. public void delTeacher()
  85. {
  86. try
  87. {
  88. string Id = context.Request.QueryString["Id"];
  89. TeacherService service = new TeacherService();
  90. service.del(service.get(typeof(Teacher), Id));
  91. context.Response.Write("1");
  92. }
  93. catch (Exception e)
  94. {
  95. context.Response.Write("0");
  96. }
  97. }
  98. public void updateTeacher()
  99. {
  100. try
  101. {
  102. Teacher Teacher = new Teacher();
  103. setValue(Teacher, context);
  104. HttpPostedFile hpf = context.Request.Files["headImgFile"];
  105. if (hpf != null)
  106. {
  107. string savepath = context.Server.MapPath("/uploadFile/headImg/" + Teacher.Id + "." + hpf.GetType());//路径,相对于服务器当前的路径
  108. hpf.SaveAs(savepath);//保存
  109. Teacher.HeadImage = savepath;
  110. }
  111. TeacherService s = new TeacherService();
  112. s.save(Teacher);
  113. context.Response.Write("1");
  114. }
  115. catch (Exception e)
  116. {
  117. context.Response.Write("0");
  118. }
  119. }
  120. public void getTeacher()
  121. {
  122. string Id = context.Request.QueryString["Id"];
  123. TeacherService service = new TeacherService();
  124. Teacher Teacher = (Teacher)service.get(typeof(Teacher), Id);
  125. String json = JsonConvert.SerializeObject(Teacher);
  126. context.Response.Write(json);
  127. }
  128. public void getTeachers()
  129. {
  130. try
  131. {
  132. TeacherService service = new TeacherService();
  133. Teacher Teacher = new Teacher();
  134. setValue(Teacher, context);
  135. int rows = Convert.ToInt32(context.Request.Form["rows"]);
  136. int page = Convert.ToInt32(context.Request.Form["page"]);
  137. object[] data = service.getTeacherList(Teacher, rows, page);
  138. Hashtable ht = new Hashtable();
  139. ht.Add("total", data[0]);
  140. ht.Add("rows", data[1]);
  141. String json = JsonConvert.SerializeObject(ht);
  142. context.Response.Write(json);
  143. }
  144. catch (Exception e)
  145. {
  146. }
  147. }
  148. public Object setValue(Object o, HttpContext context)
  149. {
  150. string[] keys = context.Request.Form.AllKeys;
  151. foreach (string s in keys)
  152. {
  153. try
  154. {
  155. PropertyInfo property = o.GetType().GetProperty(s);
  156. if (property == null)
  157. {
  158. continue;
  159. }
  160. if (property.PropertyType == typeof(DateTime))
  161. {
  162. property.SetValue(o, Convert.ToDateTime(context.Request.Form.Get(s)), null);
  163. }
  164. else if (property.PropertyType == typeof(string) || property.PropertyType == typeof(int))
  165. {
  166. property.SetValue(o, context.Request.Form.Get(s), null);
  167. }
  168. }
  169. catch (Exception e)
  170. {
  171. Console.Write(e.Message);
  172. }
  173. }
  174. return o;
  175. }
  176. }
  177. }