Advertisement
Guest User

Untitled

a guest
Feb 6th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.69 KB | None | 0 0
  1. using Backend.Models;
  2. using Backend.Models.User;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Net.Http;
  9. using System.Text;
  10. using System.Web;
  11. using System.Web.Http;
  12. using System.Web.Http.Results;
  13.  
  14. namespace Backend.Controllers
  15. {
  16. /// <summary>
  17. /// Controller for user
  18. /// </summary>
  19. [RoutePrefix("api/user")]
  20. public class UserController : ApiController
  21. {
  22. /// <summary>
  23. /// Return an array of Users
  24. /// </summary>
  25. /// <returns>Users</returns>
  26. [Route("getall")]
  27. [HttpPost]
  28. public UserModel[] GetUsers()
  29. {
  30. using (APIContext ctx = new APIContext())
  31. {
  32. UserModel[] users = ctx.Users.Where(x => x.accType == 0).ToArray();
  33. return users;
  34. }
  35. }
  36.  
  37. /// <summary>
  38. /// Return an array of Entrprises
  39. /// </summary>
  40. /// <returns>Users</returns>
  41. [Route("getallenterprise")]
  42. [HttpPost]
  43. public UserModel[] GetEnterprises([FromBody] CoordinateSearch model)
  44. {
  45. if (model == null)
  46. {
  47. model = new CoordinateSearch();
  48. Validate(model);
  49. }
  50. if (!ModelState.IsValid)
  51. {
  52. return null;
  53. }
  54.  
  55. using (APIContext ctx = new APIContext())
  56. {
  57. UserModel[] users = ctx.Users.Where(x => x.accType == 1).OrderBy(x => Math.Abs(x.xCord - model.xCord)).OrderBy(x => Math.Abs(x.yCord - model.yCord)).ToArray();
  58. return users;
  59. }
  60. }
  61.  
  62. /// <summary>
  63. /// Returns a specific user at Id
  64. /// </summary>
  65. /// <param name="id"></param>
  66. /// <returns>User</returns>
  67. [Route("getuser")]
  68. [HttpPost]
  69. public UserModel GetUser([FromBody]FindUser model)
  70. {
  71. if (model == null)
  72. {
  73. model = new FindUser();
  74. Validate(model);
  75. }
  76. if (!ModelState.IsValid)
  77. {
  78. return null;
  79. }
  80.  
  81. using (APIContext ctx = new APIContext())
  82. {
  83. if (String.IsNullOrWhiteSpace(model.name) == false)
  84. return ctx.Users.FirstOrDefault(x => x.name == model.name);
  85. if (model.id != 0)
  86. return ctx.Users.FirstOrDefault(x => x.id == model.id);
  87. }
  88.  
  89. return null;
  90. }
  91.  
  92. /// <summary>
  93. /// Update a user at a specific Id with new information.
  94. /// </summary>
  95. /// <param name="model">Model to use to update user</param>
  96. [Route("update")]
  97. [HttpPost]
  98. public HttpResponseMessage Update([FromBody] UpdateUserModel model)
  99. {
  100. if (model == null)
  101. {
  102. model = new UpdateUserModel();
  103. Validate(model);
  104. }
  105. if (!ModelState.IsValid)
  106. {
  107. return Request.CreateResponse(HttpStatusCode.BadRequest);
  108. }
  109.  
  110. string newProfilePic = "";
  111. if (model.pic != null)
  112. newProfilePic = StorageManager.UploadToStorage(model.pic);
  113.  
  114. using (APIContext ctx = new APIContext())
  115. {
  116. UserModel user = ctx.Users.FirstOrDefault(x => x.id == model.id);
  117. if(user == new UserModel() || user == null)
  118. {
  119. return Request.CreateResponse(HttpStatusCode.NotFound);
  120. }
  121.  
  122. if(String.IsNullOrWhiteSpace(model.name) == false)
  123. user.name = model.name;
  124. if (String.IsNullOrWhiteSpace(newProfilePic) == false)
  125. user.profilePic = newProfilePic;
  126. user.dateUpdated = DateTime.UtcNow;
  127. ctx.SaveChanges();
  128. }
  129.  
  130. return Request.CreateResponse(HttpStatusCode.OK);
  131. }
  132.  
  133. /// <summary>
  134. /// Creates a user
  135. /// </summary>
  136. /// <param name="model">Data to enter</param>
  137. [Route("create")]
  138. [HttpPost]
  139. public HttpResponseMessage Create([FromBody] CreateUserModel model)
  140. {
  141. if (model == null)
  142. {
  143. model = new CreateUserModel();
  144. Validate(model);
  145. }
  146. if (!ModelState.IsValid)
  147. {
  148. return Request.CreateResponse(HttpStatusCode.BadRequest);
  149. }
  150.  
  151. string newPic = "";
  152. if (model.pic != null)
  153. newPic = StorageManager.UploadToStorage(model.pic);
  154.  
  155. using (APIContext ctx = new APIContext())
  156. {
  157. if (ctx.Users.FirstOrDefault(x => x.email == model.email) != null)
  158. return Request.CreateResponse(HttpStatusCode.Conflict);
  159. UserModel newModel = new UserModel();
  160. newModel.name = model.name;
  161. newModel.accType = model.accType;
  162. newModel.xCord = model.xCord;
  163. newModel.yCord = model.yCord;
  164. newModel.email = model.email;
  165. newModel.password = model.password;
  166. newModel.profilePic = newPic;
  167. newModel.score = 0;
  168. newModel.dateCreated = DateTime.UtcNow;
  169. newModel.dateUpdated = DateTime.UtcNow;
  170. ctx.Users.Add(newModel);
  171. ctx.SaveChanges();
  172. }
  173.  
  174. return Request.CreateResponse(HttpStatusCode.OK);
  175. }
  176.  
  177. /// <summary>
  178. /// Delete a user at an id
  179. /// </summary>
  180. /// <param name="model">Model to delete</param>
  181. // DELETE api/User/5
  182. [Route("deleteuser")]
  183. [HttpPost]
  184. public HttpResponseMessage DeleteUser([FromBody]FindUser model)
  185. {
  186. if (model == null)
  187. {
  188. model = new FindUser();
  189. Validate(model);
  190. }
  191. if (!ModelState.IsValid)
  192. {
  193. return Request.CreateResponse(HttpStatusCode.BadRequest);
  194. }
  195.  
  196. using (APIContext ctx = new APIContext())
  197. {
  198. if (String.IsNullOrWhiteSpace(model.name) == false)
  199. ctx.Users.Remove(ctx.Users.FirstOrDefault(x => x.name == model.name));
  200. if (model.id != 0)
  201. ctx.Users.Remove(ctx.Users.FirstOrDefault(x => x.id == model.id));
  202. ctx.SaveChanges();
  203. }
  204.  
  205. return Request.CreateResponse(HttpStatusCode.OK);
  206. }
  207.  
  208. /// <summary>
  209. /// Log a user in using email and password
  210. /// </summary>
  211. /// <param name="authModel"></param>
  212. /// <returns>If Valid: id | If Invalid: Error</returns>
  213. // POST api/User/Login
  214. [Route("login")]
  215. [HttpPost]
  216. public HttpResponseMessage Login([FromBody] AuthModel model)
  217. {
  218. if (model == null)
  219. {
  220. model = new AuthModel();
  221. Validate(model);
  222. }
  223. if (!ModelState.IsValid)
  224. {
  225. return Request.CreateResponse(HttpStatusCode.BadRequest);
  226. }
  227.  
  228. model.password = Encryption.ComputeSha256Hash(model.password);
  229.  
  230. UserModel userModel = new UserModel();
  231.  
  232. using (APIContext ctx = new APIContext())
  233. {
  234. userModel = ctx.Users.FirstOrDefault(x => x.email == model.email && x.password == model.password);
  235. }
  236.  
  237. if (userModel != null)
  238. return Request.CreateResponse(HttpStatusCode.OK, userModel.id);
  239. else
  240. return Request.CreateResponse(HttpStatusCode.NotFound);
  241. }
  242.  
  243. /// <summary>
  244. /// Register a new User
  245. /// </summary>
  246. /// <param name="model">New data</param>
  247. /// <returns>Result</returns>
  248. // POST api/User/Register
  249. [Route("register")]
  250. [HttpPost]
  251. public HttpResponseMessage Register([FromBody] CreateUserModel model)
  252. {
  253. if(model == null)
  254. {
  255. model = new CreateUserModel();
  256. Validate(model);
  257. }
  258. if(!ModelState.IsValid)
  259. {
  260. return Request.CreateResponse(HttpStatusCode.BadRequest);
  261. }
  262.  
  263. model.password = Encryption.ComputeSha256Hash(model.password);
  264.  
  265. UserModel newModel = new UserModel();
  266. newModel.name = model.name;
  267. newModel.accType = model.accType;
  268. newModel.xCord = model.xCord;
  269. newModel.yCord = model.yCord;
  270. newModel.email = model.email;
  271. newModel.password = model.password;
  272. newModel.score = 0;
  273. newModel.dateCreated = DateTime.UtcNow;
  274. newModel.dateUpdated = DateTime.UtcNow;
  275.  
  276. if (model.pic != null)
  277. newModel.profilePic = StorageManager.UploadToStorage(model.pic);
  278. else
  279. newModel.profilePic = "";
  280.  
  281. int newId = 0;
  282. using (APIContext ctx = new APIContext())
  283. {
  284. if (ctx.Users.FirstOrDefault(x => x.email == newModel.email) != null)
  285. return Request.CreateResponse(HttpStatusCode.Conflict);
  286.  
  287. newModel = ctx.Users.Add(newModel);
  288. ctx.SaveChanges();
  289.  
  290. newId = newModel.id;
  291. }
  292.  
  293. return Request.CreateResponse(HttpStatusCode.OK, newId);
  294. }
  295. }
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement