Exemple #1
0
 private void ValidateIfObjectIsNotNull(SubjectFullServiceModel subject)
 {
     if (subject is null)
     {
         throw new ArgumentNullException("The Subject cannot be null and needs to have value.");
     }
 }
Exemple #2
0
        public async Task <SubjectFullServiceModel> CreateSubjectAsync(SubjectFullServiceModel subject)
        {
            ValidateIfObjectIsNotNull(subject);
            ValidateIfInputIsNotNullOrEmpty(subject.Name);
            ValidateIfNameAlreadyExists(subject.Name);

            Subject subjectNew = this.mapper.Map <Subject>(subject);

            await SetDefaultImagePathIfImagePathIsNull(subjectNew);
            await SetDefaultDescriptionIfDescriptionIsNull(subjectNew);

            await db.Subjects.AddAsync(subjectNew);

            await db.SaveChangesAsync();

            Subject subjectCreated = db.Subjects.FirstOrDefault(x => x.Name == subject.Name && x.Description == subjectNew.Description);

            return(this.mapper.Map <SubjectFullServiceModel>(subjectCreated));
        }
Exemple #3
0
        public async Task <SubjectFullServiceModel> UpdateSubjectAsync(SubjectFullServiceModel subject)
        {
            ValidateSubjectId(subject.Id);
            ValidateIfSubjectIsDeleted(subject.Id);
            ValidateIfInputIsNotNullOrEmpty(subject.Name);

            var subjectInDb = await db.Subjects.FindAsync(subject.Id);

            subjectInDb.Name        = subject.Name;
            subjectInDb.Description = subject.Description;
            subjectInDb.Image       = subject.Image;
            await db.SaveChangesAsync();

            var subjectInDbUpdated = await db.Subjects
                                     .Include(x => x.Classes)
                                     .ThenInclude(subjectClass => subjectClass.Class)
                                     .Include(x => x.Teachers)
                                     .ThenInclude(subjectTeacher => subjectTeacher.Teacher)
                                     .FirstOrDefaultAsync(x => x.Id == subject.Id);

            return(this.mapper.Map <SubjectFullServiceModel>(subjectInDbUpdated));
        }
        public async Task <IActionResult> Post([FromBody] SubjectFullServiceModel subject)
        {
            var subjectCreated = await this.subjectService.CreateSubjectAsync(subject);

            return(Ok(subjectCreated));
        }