Exemple #1
0
        public async Task <IActionResult> AddLecturer(int userId, LecturersDto lecturersDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var lecturersFromRepo = await _repo.GetLecturers();

            foreach (var lecturer in lecturersFromRepo)
            {
                if (lecturer.Name.Trim() == lecturersDto.Name.Trim())
                {
                    return(BadRequest($"lecturer {lecturer.Name} already exists"));
                }
            }

            lecturersDto.Name = lecturersDto.Name.Trim();
            var newLecturer = _mapper.Map <Lecturer>(lecturersDto);

            _repo.Add(newLecturer);
            if (await _repo.SaveAll())
            {
                var lecturerToReturn = _mapper.Map <LecturersDto>(newLecturer);
                return(CreatedAtRoute("GetLecturer"
                                      , new { userId = userId, id = newLecturer.Id }
                                      , lecturerToReturn));
            }
            return(BadRequest("Failed to add the lecturer"));
        }
Exemple #2
0
        public async Task <IActionResult> AddCourse(int userId, CourseDto courseDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var coursesFromRepo = await _repo.GetCourses();

            foreach (var course in coursesFromRepo)
            {
                if (course.Title.Trim() == courseDto.Title.Trim())
                {
                    return(BadRequest($"student {course.Title} already exists"));
                }
            }
            courseDto.Title = courseDto.Title.Trim();
            var newCourse = _mapper.Map <Course>(courseDto);

            _repo.Add(newCourse);
            if (await _repo.SaveAll())
            {
                var courseToReturn = _mapper.Map <CourseDto>(newCourse);
                return(CreatedAtRoute("GetCourse"
                                      , new { userId = userId, id = newCourse.Id }
                                      , courseToReturn));
            }
            return(BadRequest("Failed to add the course"));
        }
Exemple #3
0
        public async Task <IActionResult> AddOrganizationType(int userId, OrganizationTypeDto organizationTypeDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var otFromRepo = await _repo.GetOrganizationTypes();

            foreach (var ot in otFromRepo)
            {
                if (ot.Title.Trim() == organizationTypeDto.Title.Trim())
                {
                    return(BadRequest($"organization type {ot.Title} already exists"));
                }
            }
            organizationTypeDto.Title = organizationTypeDto.Title.Trim();
            var newOt = _mapper.Map <OrganizationType>(organizationTypeDto);

            _repo.Add(newOt);
            if (await _repo.SaveAll())
            {
                var otToReturn = _mapper.Map <OrganizationTypeDto>(newOt);
                return(CreatedAtRoute("GetOrganizationType"
                                      , new { userId = userId, id = newOt.Id }
                                      , otToReturn));
            }
            return(BadRequest("Failed to add the organization type"));
        }
        public async Task <IActionResult> AddProductType(int userId, ProductTypeDto productTypeDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var ptFromRepo = await _repo.GetTypes();

            foreach (var pt in ptFromRepo)
            {
                if (pt.Title.Trim() == productTypeDto.Title.Trim())
                {
                    return(BadRequest($"type {productTypeDto.Title} already exists"));
                }
            }
            productTypeDto.Title = productTypeDto.Title.Trim();
            var newPt = _mapper.Map <ProductType>(productTypeDto);

            _repo.Add(newPt);
            if (await _repo.SaveAll())
            {
                var ptToReturn = _mapper.Map <ProductTypeDto>(newPt);
                return(CreatedAtRoute("GetProductType"
                                      , new { userId = userId, id = newPt.Id }
                                      , ptToReturn));
            }
            return(BadRequest("Failed to add the type"));
        }
Exemple #5
0
        public async Task <IActionResult> AddOrganization(int userId, OrganizationDto organizationDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var organizationsFromRepo = await _repo.GetOrganizations();

            foreach (var org in organizationsFromRepo)
            {
                if (org.Title.Trim() == organizationDto.Title.Trim())
                {
                    return(BadRequest($"organization {org.Title} already exists"));
                }
            }
            if (organizationDto.OrganizationTypes.Count == 0)
            {
                return(BadRequest("no types selected"));
            }

            organizationDto.Title = organizationDto.Title.Trim();
            var newOrganization = _mapper.Map <Organization>(organizationDto);

            _repo.Add(newOrganization);

            if (await _repo.SaveAll())
            {
                var organizationToReturn = _mapper.Map <OrganizationDto>(newOrganization);
                return(CreatedAtRoute("GetOrganization"
                                      , new { userId = userId, id = newOrganization.Id }
                                      , organizationToReturn));
            }
            return(BadRequest("Failed to add the organization"));
        }
Exemple #6
0
        public async Task <IActionResult> AddTag(int userId, TagDto tagDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var tagsFromRepo = await _repo.GetTags();

            foreach (var tag in tagsFromRepo)
            {
                if (tag.Title.Trim() == tagDto.Title.Trim())
                {
                    return(BadRequest($"tag {tag.Title} already exists"));
                }
            }
            tagDto.Title = tagDto.Title.Trim();
            var newTag = _mapper.Map <Tag>(tagDto);

            _repo.Add(newTag);
            if (await _repo.SaveAll())
            {
                var tagToReturn = _mapper.Map <TagDto>(newTag);
                return(CreatedAtRoute("GetTag"
                                      , new { userId = userId, id = newTag.Id }
                                      , tagToReturn));
            }
            return(BadRequest("Failed to add the tag"));
        }
Exemple #7
0
        public async Task <IActionResult> AddStudent(int userId, StudentDto studentDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var studentsFromRepo = await _repo.GetStudents();

            foreach (var student in studentsFromRepo)
            {
                if (student.Name.Trim() == studentDto.Name.Trim())
                {
                    return(BadRequest($"student {student.Name} already exists"));
                }
            }
            studentDto.Name = studentDto.Name.Trim();
            var newStudent = _mapper.Map <Student>(studentDto);

            _repo.Add(newStudent);
            if (await _repo.SaveAll())
            {
                var studentToReturn = _mapper.Map <StudentDto>(newStudent);
                return(CreatedAtRoute("GetStudent"
                                      , new { userId = userId, id = newStudent.Id }
                                      , studentToReturn));
            }
            return(BadRequest("Failed to add the student"));
        }