Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. /// <summary>
  2. /// Handles all of the Upload functions, including GetAll, Get and Create.
  3. /// </summary>
  4. [RoutePrefix("Api/Uploads")]
  5. public class UploadsController : BaseController
  6. {
  7. private readonly UploadService service;
  8.  
  9. /// <summary>
  10. /// Parameterless constructor in which the upload service is instantiated.
  11. /// </summary>
  12. public UploadsController()
  13. {
  14. this.service = new UploadService(UnitOfWork);
  15. }
  16.  
  17. // GET api/uploads
  18. /// <summary>
  19. /// Gets a list of all the Uploads.
  20. /// </summary>
  21. /// <returns>A list of uploads.</returns>
  22. [HttpGet]
  23. [Route("")]
  24. [ResponseType(typeof(IList<UploadViewModel>))]
  25. public async Task<IHttpActionResult> Get()
  26. {
  27. try
  28. {
  29. var uploads = await this.service.GetAllAsync();
  30. var models = uploads.Select(model => new UploadViewModel(model)).ToList();
  31.  
  32. return Ok(models);
  33. }
  34. catch (Exception ex)
  35. {
  36. return InternalServerError(ex);
  37. }
  38. }
  39.  
  40. // GET api/uploads
  41. /// <summary>
  42. /// Gets an upload by the required parameter; id.
  43. /// </summary>
  44. /// <param name="id">The required id paramter of the upload.</param>
  45. /// <returns>An upload view model.</returns>
  46. [HttpGet]
  47. [Route("{id:int}")]
  48. [ResponseType(typeof(UploadViewModel))]
  49. public async Task<IHttpActionResult> Get(int id)
  50. {
  51. try
  52. {
  53. var model = new UploadViewModel(await this.service.GetAsync(id));
  54.  
  55. return Ok(model);
  56. }
  57. catch (Exception ex)
  58. {
  59. return InternalServerError(ex);
  60. }
  61. }
  62.  
  63. // POST api/uploads
  64. /// <summary>
  65. /// Creates an Upload.
  66. /// </summary>
  67. /// <param name="model">The model representing the upload.</param>
  68. /// <returns>Nothing.</returns>
  69. [HttpPost]
  70. [Route("")]
  71. [Authorize]
  72. public async Task<IHttpActionResult> Post(UploadBindingViewModel model)
  73. {
  74. if (!ModelState.IsValid)
  75. return BadRequest(ModelState);
  76.  
  77. var upload = new Upload
  78. {
  79. Id = model.Id,
  80. Name = model.Name,
  81. Url = model.Url
  82. };
  83.  
  84. try
  85. {
  86. this.service.Create(upload);
  87.  
  88. await this.UnitOfWork.SaveChangesAsync();
  89. }
  90. catch (Exception ex)
  91. {
  92. return InternalServerError(ex);
  93. }
  94.  
  95. return Ok(upload.Id);
  96. }
  97.  
  98. // DELETE api/uploads
  99. /// <summary>
  100. /// Deletes an upload.
  101. /// </summary>
  102. /// <param name="id">The id of the upload.</param>
  103. /// <returns></returns>
  104. [HttpDelete]
  105. [Route("{id:int}")]
  106. [Authorize]
  107. public async Task<IHttpActionResult> Delete(int id)
  108. {
  109. try
  110. {
  111. await this.service.RemoveAsync(id);
  112.  
  113. await this.UnitOfWork.SaveChangesAsync();
  114. }
  115. catch (Exception ex)
  116. {
  117. return InternalServerError(ex);
  118. }
  119.  
  120. return Ok();
  121. }
  122. }
  123.  
  124. // DELETE api/uploads
  125. /// <summary>
  126. /// Deletes an upload.
  127. /// </summary>
  128. /// <param name="id">The id of the upload.</param>
  129. /// <returns></returns>
  130. [HttpDelete]
  131. [Route("{id:int}")]
  132. [Authorize]
  133. public async Task<IHttpActionResult> Delete(int id)
  134. {
  135. try
  136. {
  137. await this.service.RemoveAsync(id);
  138.  
  139. await this.UnitOfWork.SaveChangesAsync();
  140. }
  141. catch (Exception ex)
  142. {
  143. return InternalServerError(ex);
  144. }
  145.  
  146. return Ok();
  147. }
  148.  
  149. $.ajax({
  150. type: "DELETE",
  151. url: uploadUrl + "?id=" + self.id()
  152. }).fail(function () {
  153. toastr.error("Failed to remove the upload from our system.");
  154. });
  155.  
  156. $.ajax({
  157. type: "DELETE",
  158. url: uploadUrl,
  159. data: { id: self.id() }
  160. }).fail(function () {
  161. toastr.error("Failed to remove the upload from our system.");
  162. });
  163.  
  164. // DELETE api/uploads
  165. /// <summary>
  166. /// Deletes an upload.
  167. /// </summary>
  168. /// <param name="id">The id of the upload.</param>
  169. /// <returns></returns>
  170. [HttpDelete]
  171. [Route("")]
  172. [Authorize]
  173. public async Task<IHttpActionResult> Delete(int id)
  174. {
  175. try
  176. {
  177. await this.service.RemoveAsync(id);
  178.  
  179. await this.UnitOfWork.SaveChangesAsync();
  180. }
  181. catch (Exception ex)
  182. {
  183. return InternalServerError(ex);
  184. }
  185.  
  186. return Ok();
  187. }
  188.  
  189. $.ajax({
  190. type: "DELETE",
  191. url: uploadUrl + "?id=" + self.id()
  192. }).fail(function () {
  193. toastr.error("Failed to remove the upload from our system.");
  194. });
  195.  
  196. $.ajax({
  197. type: "DELETE",
  198. url: uploadUrl,
  199. data: { id: self.id() }
  200. }).fail(function () {
  201. toastr.error("Failed to remove the upload from our system.");
  202. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement