public async Task <ClassFullServiceModel> CreateClassAsync(ClassFullServiceModel classInput) { ValidateIfObjectIsNotNull(classInput); ValidateIfInputIsNotNullOrEmpty(classInput.Name); ValidateIfNameAlreadyExists(classInput.Name); Teacher teacherOfClassInput = await this.db.Teachers.FirstOrDefaultAsync(x => x.Id == classInput.MasterTeacher.Id); Class classToCreate = new Class { Name = classInput.Name, Description = classInput.Description, Image = classInput.Image, IsDeleted = false, IsUnlisted = false, MasterTeacher = teacherOfClassInput, MasterTeacherId = teacherOfClassInput.Id, }; await SetDefaultImagePathIfImagePathIsNull(classToCreate); await SetDefaultDescriptionIfDescriptionIsNull(classToCreate); await db.Classes.AddAsync(classToCreate); await db.SaveChangesAsync(); Class classCreated = db.Classes.FirstOrDefault(x => x.Name == classInput.Name && x.Description == classToCreate.Description); return(this.mapper.Map <ClassFullServiceModel>(classCreated)); }
private void ValidateIfObjectIsNotNull(ClassFullServiceModel classy) { if (classy is null) { throw new ArgumentNullException("The Class cannot be null and needs to have value."); } }
public async Task <ClassFullServiceModel> UpdateClassAsync(ClassFullServiceModel classToUpdate) { ValidateClassId(classToUpdate.Id); ValidateIfClassIsDeleted(classToUpdate.Id); ValidateIfInputIsNotNullOrEmpty(classToUpdate.Name); var classInDb = await db.Classes.FindAsync(classToUpdate.Id); classInDb.Name = classToUpdate.Name; classInDb.Description = classToUpdate.Description; classInDb.Image = classToUpdate.Image; await db.SaveChangesAsync(); var classInDbUpdated = await db.Classes .Include(x => x.MasterTeacher) .Include(x => x.Students) .Include(x => x.Subjects) .Include(x => x.Teachers) .FirstOrDefaultAsync(x => x.Id == classToUpdate.Id); return(this.mapper.Map <ClassFullServiceModel>(classInDbUpdated)); }
public async Task <IActionResult> Post([FromBody] ClassFullServiceModel classToCreate) { var classCreated = await this.classService.CreateClassAsync(classToCreate); return(Ok(classCreated)); }