コード例 #1
0
        public async Task <IActionResult> AddSubjectInClass([FromForm] SubjectForClassCreationDto subjectForClassCreationDto)
        {
            if (subjectForClassCreationDto == null)
            {
                return(BadRequest());
            }

            if (!await _repo.IsExistClass(subjectForClassCreationDto.ClassId))
            {
                return(BadRequest("Class Doesn't Exist"));
            }

            var createdClass = await _repo.GetClassById(subjectForClassCreationDto.ClassId);

            if (!await _repo.IsExistSubject(subjectForClassCreationDto.SubjectId))
            {
                return(BadRequest("Subject Doesn't Exist"));
            }

            if (await _repo.IsExistSubjectInClass(subjectForClassCreationDto.ClassId, subjectForClassCreationDto.SubjectId))
            {
                return(BadRequest("Subject Exist in this class"));
            }


            var subject = await _repo.GetSubjectById(subjectForClassCreationDto.SubjectId);

            var subjectImage = subjectForClassCreationDto.SubjectImage;

            var imagePath = _repo.AddImage(subjectImage, "Subject");

            var createdSubjectForClass = _mapper.Map <SubjectForClass>(subjectForClassCreationDto);

            createdSubjectForClass.ClassName   = createdClass.Name;
            createdSubjectForClass.SubjectName = subject.Name;
            createdSubjectForClass.VirtualPath = imagePath.VirtualPath;
            createdSubjectForClass.FileName    = imagePath.FileName;
            createdSubjectForClass.CreatedDate = DateTime.Now;

            _repo.Add <SubjectForClass>(createdSubjectForClass);

            if (await _repo.SaveAll())
            {
                return(Ok());
            }

            return(BadRequest("Failed to add subject in the class"));
        }