Esempio n. 1
0
        public ActionResult UpdateMark(UpdateMarkModel model)
        {
            if (ModelState.IsValid)
            {
                if (!IsKeyValid(model))
                {
                    return(StatusCode(resp.code, resp));
                }
                else
                {
                    // get result filter with resultDetail.month
                    var result = _resultService.GetResult(model.StudentID, model.SubjectID, model.SemesterID, model.Month);

                    // Create new result if not exist
                    if (result == null)
                    {
                        result = new Result
                        {
                            StudentID  = model.StudentID,
                            SemesterID = model.SemesterID,
                            SubjectID  = model.SubjectID
                        };

                        _resultService.CreateResult(result);

                        // get again for included entities
                        result = _resultService.GetResults(model.StudentID, model.SemesterID, model.SubjectID, null)
                                 .AsQueryable()
                                 .FirstOrDefault();
                    }

                    // details
                    var detail = _resultService.GetResultDetail(result.ResultID, model.ResultTypeID, model.Month);

                    // create detail if not exist
                    if (detail == null)
                    {
                        // Check for valid month - semester
                        if (result.Semester.Label == 1)
                        {
                            if (!(model.Month >= 9 && model.Month <= 12))
                            {
                                resp.code = 400; // Bad Request
                                resp.messages.Add(new { Month = "Month " + model.Month + " - Semester " + result.Semester.Label + " is invalid" });
                                return(BadRequest(resp));
                            }
                        }
                        else
                        {
                            if (!(model.Month >= 2 && model.Month <= 5))
                            {
                                resp.code = 400; // Bad Request
                                resp.messages.Add(new { Month = "Month " + model.Month + " - Semester " + result.Semester.Label + " is invalid" });
                                return(BadRequest(resp));
                            }
                        }

                        _resultService.CreateResultDetails(result, model.Month);
                        detail = _resultService.GetResultDetail(result.ResultID, model.ResultTypeID, model.Month);
                    }

                    // update mark
                    detail.Mark = model.Mark;
                    _resultService.Update();

                    _resultService.CalculateSubjectMonthlyAverage(result);

                    return(Ok(detail));
                }
            }
            else // ModelState is invalid
            {
                var errors = new List <string>();
                foreach (var state in ModelState)
                {
                    foreach (var error in state.Value.Errors)
                    {
                        errors.Add(error.ErrorMessage);
                    }
                }
                return(BadRequest(errors));
            }
        }