Ejemplo n.º 1
0
        public IActionResult Update([FromBody] GroupsController.UpdateGroupBinding obj)
        {
            if (obj == null)
            {
                return(Ok(new { status = "parameter error" }));
            }

            if (obj.timespan != null)
            {
                if (obj.timespan.Period == null)
                {
                    return(Ok(new { status = "parameter error" }));
                }

                if (obj.timespan.Period.Value == 0 && (obj.timespan.StartDate == null || obj.timespan.EndDate == null))
                {
                    return(Ok(new { status = "parameter error" }));
                }

                if (obj.timespan.Period.Value != 0 && (obj.timespan.TimeStart == null || obj.timespan.TimeEnd == null || obj.timespan.DayOfWeek == null))
                {
                    return(Ok(new { status = "parameter error" }));
                }
            }

            try
            {
                //konvertovanje u timeSpan
                TimeSpans ts = Services.TimeSpan.getTimeSpan(obj.timespan);

                //dodavanje groupe
                if (obj.groupID == null)
                {
                    if (obj.divisionID == null)
                    {
                        return(Ok(new { status = "parameter error" }));
                    }
                    //provera konzistentnosti raspodele
                    //Data.Group.CheckConsistencyWithOtherGroups(null, obj.students.ToList());

                    Groups newGroup = groupService.Create(obj.divisionID.Value, obj.name, obj.classroomID,
                                                          ts);
                    studentService.ChangeStudents(newGroup.GroupId, obj.students.ToList());
                    if (obj.assistantID != null)
                    {
                        groupService.SetAsstant(newGroup.GroupId, obj.assistantID.Value);
                    }
                }
                else //update grupe
                {
                    //provera konzistentnosti raspodele
                    //Data.Group.CheckConsistencyWithOtherGroups(obj.groupID.Value, obj.students.ToList());


                    studentService.Update(obj.groupID.Value, obj.name, obj.classroomID, ts);
                    studentService.ChangeStudents(obj.groupID.Value, obj.students.ToList());
                    if (obj.assistantID != null)
                    {
                        groupService.SetAsstant(obj.groupID.Value, obj.assistantID.Value);
                    }
                }

                return(Ok(new { status = "success" }));
            }
            catch (InconsistentDivisionException ex)
            {
                return(Ok(new { status = "inconsistent division", message = ex.Message }));
            }
            catch (Exception ex)
            {
                return(Ok(new { status = "error", message = ex.Message }));
            }
        }