Esempio n. 1
0
        public IHttpActionResult AddStudentToGroup(string id)
        {
            var teacherId = User.Identity.GetUserId();
            var teacher = Data.Teachers.Find(teacherId);
            var student = Data.Students.All().FirstOrDefault(s => s.UserName == id);

            if (teacher == null || student == null)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, ModelState));
            }

            if (teacher.Group == null)
            {
                var group = new Group();
                group.Teacher = teacher;
                Data.Groups.Add(group);
                Data.Groups.SaveChanges();
                teacher.Group = group;
            }

            teacher.Group.Teacher = teacher;
            teacher.Group.Students.Add(student);
            student.Group = teacher.Group;
            Data.Groups.SaveChanges();

            return Ok();
        }
Esempio n. 2
0
        public GroupViewModel GetGroup()
        {
            var userId = User.Identity.GetUserId();
            ApplicationUser user;
            bool isTeacher = false;

            if (Data.Students.All().Any(s => s.Id == userId))
            {
                user = Data.Students.Find(userId);
            }
            else
            {
                user = Data.Teachers.Find(userId);
                isTeacher = true;
            }

            var group = user.Group;

            if (group == null && isTeacher)
            {
                group = new Group()
                {
                    MaxDistance = AppConstants.MaxDistance,
                    Teacher = user as Teacher,
                    TeacherId = user.Id
                };

                this.Data.Groups.Add(group);
                user.Group = group;
                this.Data.Groups.SaveChanges();
            }

            var groupViewModel = Mapper.Map<GroupViewModel>(group);

            return groupViewModel;
        }