PageRenderTime 57ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/Gradebook/Controllers/TasksController.cs

https://bitbucket.org/academium/gradebook
C# | 165 lines | 126 code | 32 blank | 7 comment | 35 complexity | 5b67bc8b74470c8df216421b42f9f1ae MD5 | raw file
  1. using AutoMapper;
  2. using Gradebook.Contracts;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Http;
  8. using System.Web.Http;
  9. namespace Gradebook.Controllers
  10. {
  11. public class TasksController : ApiControllerBase
  12. {
  13. public TasksController(IGradebookUow uow) : base(uow) { }
  14. // GET /api/tasks
  15. public IEnumerable<ViewModels.Task> Get(int? code = 0, int? term = 0, int? type = 0)
  16. {
  17. var tasks = Unit.Tasks.GetAll().AsEnumerable();
  18. if (code.HasValue || term.HasValue)
  19. {
  20. if (term.HasValue && term != 0)
  21. {
  22. if (!Unit.Terms.Contains(term.Value))
  23. throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
  24. tasks = Unit.Tasks.GetByTerm(term.Value);
  25. }
  26. if (code.HasValue && code != 0)
  27. {
  28. if (!Unit.ClassCodes.Contains(code.Value))
  29. throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
  30. tasks = tasks.Where(x => x.CodeId == code.Value);
  31. }
  32. if (type.HasValue && type != 0)
  33. {
  34. if (!Unit.TaskTypes.Contains(type.Value))
  35. throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
  36. tasks = tasks.Where(x => x.TypeId == type.Value);
  37. }
  38. }
  39. var model = tasks.Select(Mapper.Map<Model.Task, ViewModels.Task>).ToList();
  40. model.ForEach(x =>
  41. {
  42. x.Average = Unit.Grades.GetAverageByTask(x.Id);
  43. x.Status = Unit.Tasks.GetStatus(x.Id);
  44. });
  45. return model;
  46. }
  47. // GET /api/tasks/{int}
  48. [ActionName("Default")]
  49. public ViewModels.Task Get(int id)
  50. {
  51. var task = Unit.Tasks.Read(id);
  52. if (task == null)
  53. throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
  54. var model = Mapper.Map<Model.Task, ViewModels.Task>(task);
  55. model.Average = Unit.Grades.GetAverageByTask(id);
  56. model.Status = Unit.Tasks.GetStatus(id);
  57. return model;
  58. }
  59. // POST /api/tasks
  60. public HttpResponseMessage Post(ViewModels.Task model)
  61. {
  62. if (ModelState.IsValid)
  63. {
  64. var task = Mapper.Map<ViewModels.Task, Model.Task>(model);
  65. Unit.Tasks.Create(task);
  66. Unit.Commit();
  67. var response = Request.CreateResponse(HttpStatusCode.Created, model);
  68. var uri = Url.Link(WebApiConfig.ControllerAndId, new { id = task.Id });
  69. if (uri != null) response.Headers.Location = new Uri(Request.RequestUri, uri);
  70. return response;
  71. }
  72. throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
  73. }
  74. // PUT /api/tasks/{int}
  75. [ActionName("Default")]
  76. public HttpResponseMessage Put([FromUri]int id, [FromBody]ViewModels.Task model)
  77. {
  78. if (!Unit.Tasks.Contains(id))
  79. throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
  80. if (ModelState.IsValid)
  81. {
  82. var task = Unit.Tasks.Read(model.Id);
  83. task.CodeId = model.CodeId;
  84. task.AssessmentId = model.AssessmentId;
  85. task.TypeId = model.TypeId;
  86. task.TotalMarks = model.TotalMarks;
  87. task.Weight = model.Weight;
  88. task.Description = model.Description;
  89. task.StartDate = model.StartDate;
  90. task.DueDate = model.DueDate;
  91. Unit.Tasks.Update(task);
  92. Unit.Commit();
  93. return new HttpResponseMessage(HttpStatusCode.NoContent);
  94. }
  95. throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
  96. }
  97. // DELETE /api/tasks/{int}
  98. [HttpDelete]
  99. [ActionName("Default")]
  100. public void Delete(int id)
  101. {
  102. if (!Unit.Tasks.Contains(id))
  103. throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
  104. Unit.Tasks.Delete(id);
  105. Unit.Commit();
  106. }
  107. // GET /api/tasks/linked
  108. [ActionName("Linked")]
  109. public IEnumerable<ViewModels.Task> GetLinkedTasks(int assessmentId)
  110. {
  111. if (!Unit.Tasks.Contains(assessmentId))
  112. throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
  113. var tasks = Unit.Tasks.GetByAssessment(assessmentId);
  114. return tasks.Select(Mapper.Map<Model.Task, ViewModels.Task>);
  115. }
  116. // GET /api/tasks/assessments
  117. [ActionName("Assessments")]
  118. public IEnumerable<ViewModels.TaskAssessment> GetAssessmentLinks(int classId, int subjectId, int? termId = 0)
  119. {
  120. if (!Unit.Classes.Contains(classId) || !Unit.Subjects.Contains(classId))
  121. throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
  122. if (!Unit.Terms.Contains(termId.GetValueOrDefault()))
  123. throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
  124. var assessments = Unit.TaskAssessments.GetAll()
  125. .Where(x => x.ClassId == classId && x.SubjectId == subjectId);
  126. if (termId.HasValue && termId != 0)
  127. {
  128. assessments = assessments.Where(x => x.TermId == termId);
  129. }
  130. return assessments.Select(Mapper.Map<Model.TaskAssessment, ViewModels.TaskAssessment>);
  131. }
  132. }
  133. }