/Gradebook/Controllers/TasksController.cs
C# | 165 lines | 126 code | 32 blank | 7 comment | 35 complexity | 5b67bc8b74470c8df216421b42f9f1ae MD5 | raw file
- using AutoMapper;
- using Gradebook.Contracts;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
-
- namespace Gradebook.Controllers
- {
- public class TasksController : ApiControllerBase
- {
- public TasksController(IGradebookUow uow) : base(uow) { }
-
- // GET /api/tasks
- public IEnumerable<ViewModels.Task> Get(int? code = 0, int? term = 0, int? type = 0)
- {
- var tasks = Unit.Tasks.GetAll().AsEnumerable();
-
- if (code.HasValue || term.HasValue)
- {
- if (term.HasValue && term != 0)
- {
- if (!Unit.Terms.Contains(term.Value))
- throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
-
- tasks = Unit.Tasks.GetByTerm(term.Value);
- }
- if (code.HasValue && code != 0)
- {
- if (!Unit.ClassCodes.Contains(code.Value))
- throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
-
- tasks = tasks.Where(x => x.CodeId == code.Value);
- }
- if (type.HasValue && type != 0)
- {
- if (!Unit.TaskTypes.Contains(type.Value))
- throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
-
- tasks = tasks.Where(x => x.TypeId == type.Value);
- }
- }
-
- var model = tasks.Select(Mapper.Map<Model.Task, ViewModels.Task>).ToList();
- model.ForEach(x =>
- {
- x.Average = Unit.Grades.GetAverageByTask(x.Id);
- x.Status = Unit.Tasks.GetStatus(x.Id);
- });
- return model;
- }
-
- // GET /api/tasks/{int}
- [ActionName("Default")]
- public ViewModels.Task Get(int id)
- {
- var task = Unit.Tasks.Read(id);
-
- if (task == null)
- throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
-
- var model = Mapper.Map<Model.Task, ViewModels.Task>(task);
- model.Average = Unit.Grades.GetAverageByTask(id);
- model.Status = Unit.Tasks.GetStatus(id);
-
- return model;
- }
-
- // POST /api/tasks
- public HttpResponseMessage Post(ViewModels.Task model)
- {
- if (ModelState.IsValid)
- {
- var task = Mapper.Map<ViewModels.Task, Model.Task>(model);
-
- Unit.Tasks.Create(task);
- Unit.Commit();
-
- var response = Request.CreateResponse(HttpStatusCode.Created, model);
- var uri = Url.Link(WebApiConfig.ControllerAndId, new { id = task.Id });
- if (uri != null) response.Headers.Location = new Uri(Request.RequestUri, uri);
-
- return response;
- }
-
- throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
- }
-
- // PUT /api/tasks/{int}
- [ActionName("Default")]
- public HttpResponseMessage Put([FromUri]int id, [FromBody]ViewModels.Task model)
- {
- if (!Unit.Tasks.Contains(id))
- throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
-
- if (ModelState.IsValid)
- {
- var task = Unit.Tasks.Read(model.Id);
-
- task.CodeId = model.CodeId;
- task.AssessmentId = model.AssessmentId;
- task.TypeId = model.TypeId;
- task.TotalMarks = model.TotalMarks;
- task.Weight = model.Weight;
- task.Description = model.Description;
- task.StartDate = model.StartDate;
- task.DueDate = model.DueDate;
-
- Unit.Tasks.Update(task);
- Unit.Commit();
-
- return new HttpResponseMessage(HttpStatusCode.NoContent);
- }
-
- throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
- }
-
- // DELETE /api/tasks/{int}
- [HttpDelete]
- [ActionName("Default")]
- public void Delete(int id)
- {
- if (!Unit.Tasks.Contains(id))
- throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
-
- Unit.Tasks.Delete(id);
- Unit.Commit();
- }
-
- // GET /api/tasks/linked
- [ActionName("Linked")]
- public IEnumerable<ViewModels.Task> GetLinkedTasks(int assessmentId)
- {
- if (!Unit.Tasks.Contains(assessmentId))
- throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
-
- var tasks = Unit.Tasks.GetByAssessment(assessmentId);
-
- return tasks.Select(Mapper.Map<Model.Task, ViewModels.Task>);
- }
-
- // GET /api/tasks/assessments
- [ActionName("Assessments")]
- public IEnumerable<ViewModels.TaskAssessment> GetAssessmentLinks(int classId, int subjectId, int? termId = 0)
- {
- if (!Unit.Classes.Contains(classId) || !Unit.Subjects.Contains(classId))
- throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
-
- if (!Unit.Terms.Contains(termId.GetValueOrDefault()))
- throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
-
- var assessments = Unit.TaskAssessments.GetAll()
- .Where(x => x.ClassId == classId && x.SubjectId == subjectId);
-
- if (termId.HasValue && termId != 0)
- {
- assessments = assessments.Where(x => x.TermId == termId);
- }
-
- return assessments.Select(Mapper.Map<Model.TaskAssessment, ViewModels.TaskAssessment>);
- }
- }
- }