Exemple #1
0
        public async Task <IActionResult> GetLecturerByEmail(string email)
        {
            var lecturer = await lecturerRepository.GetLecturerByEmail(email, true);

            if (lecturer == null)
            {
                return(NotFound());
            }

            var lecturerResource = mapper.Map <Lecturer, LecturerResource>(lecturer);

            return(Ok(lecturerResource));
        }
Exemple #2
0
        public async Task <IActionResult> CreateGroup([FromBody] GroupResource groupResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            //case: lecturer, project and major is not the same
            // var checkGroup = await groupRepository.CheckGroup(groupResource);
            // if (!checkGroup)
            // {
            //     ModelState.AddModelError("Error", "Stop hacking. Please set the Lecturer, project and group in the same Major.");
            //     return BadRequest(ModelState);
            // }

            var group = mapper.Map <GroupResource, Group>(groupResource);

            group.Lecturer = await lecturerRepository.GetLecturerByEmail(groupResource.LecturerEmail);

            if (groupResource.ProjectId == null)
            {
                var otherProject = mapper.Map <ProjectResource, Project>(groupResource.OtherProject);
                group.Project = otherProject;
            }
            else
            {
                group.Project = await projectRepository.GetProject(groupResource.ProjectId);
            }

            var major = await majorRepository.GetMajor(groupResource.MajorId);

            group.Major = major;

            var quarter = await quarterRepository.GetQuarter(groupResource.QuarterId);

            group.Quarter = quarter;

            groupRepository.UpdateEnrollments(group, groupResource);
            groupRepository.UpdateUploadedFiles(group, groupResource);

            groupRepository.AddGroup(group);
            await unitOfWork.Complete();

            group = await groupRepository.GetGroup(group.GroupId);

            //foreach (var enrollmentResource in groupResource.Enrollments)
            //{
            //    var enrollment = await enrollmentRepository.GetEnrollment(enrollmentResource.EnrollmentId);
            //    //case: enrollment's type and project's type is different and the student has been already in group

            //    if (group != null && group.Project.Type != enrollment.Type)
            //    {
            //        ModelState.AddModelError("Error", "Enrollment's type and Project Type of Group are not the same.");
            //        groupRepository.RemoveGroup(group);
            //        await unitOfWork.Complete();
            //        return BadRequest(ModelState);
            //    }
            //    else if (group != null && !groupRepository.CheckEnrollment(group, enrollment))
            //    {
            //        ModelState.AddModelError("Warning", "This group already has this student.");
            //        groupRepository.RemoveGroup(group);
            //        await unitOfWork.Complete();
            //        return BadRequest(ModelState);
            //    }
            //    else
            //    {
            //        enrollment.Group = group;
            //        group.Enrollments.Add(enrollment);
            //        await unitOfWork.Complete();
            //    }

            //}

            var result = mapper.Map <Group, GroupResource>(group);

            return(Ok(result));
        }