/// <summary> /// Converts DTO object to Entity, and adds it to database, if there is no element that satify condition. /// </summary> public async Task <ResultMessage <TDto> > Add(TDto dto, Predicate <TEntity> condition) { try { TEntity entity = await GetSingleOrDefault(condition); if (entity != null) { return(new ResultMessage <TDto>(OperationStatus.Exists)); } await _context.Set <TEntity>().AddAsync(Mapping.Mapper.Map <TEntity>(dto)); await _context.SaveChangesAsync(); entity = await GetSingleOrDefault(condition); return(new ResultMessage <TDto>(Mapping.Mapper.Map <TDto>(entity))); } catch (DbUpdateException ex) { return(new ResultMessage <TDto>(_errorHandler.Handle(ex))); } catch (AutoMapperMappingException ex) { return(new ResultMessage <TDto>(_errorHandler.Handle(ex))); } catch (Exception ex) { return(new ResultMessage <TDto>(_errorHandler.Handle(ex))); } }
/// <summary> /// Related specific course with specific study program, and semester. /// </summary> /// <param name="courseId">Unique identifer for the course</param> /// <param name="studyProgram">Study program that needs to be related with specified course</param> /// <param name="semester">Semester that needs to be related with specified course</param> /// <returns>True if added, false if not</returns> public async Task <ResultMessage <bool> > AddInStudyProgram(int courseId, StudyProgram studyProgram, Semester semester) { try { CourseStudyProgram courseStudyProgram = new CourseStudyProgram() { CourseId = courseId, Semester = semester, StudyProgram = studyProgram, StudyYear = semester.GetStudyYear() }; await _orhedgeContext.AddAsync(courseStudyProgram); await _orhedgeContext.SaveChangesAsync(); return(new ResultMessage <bool>(true, OperationStatus.Success)); } catch (DbUpdateException ex) { return(new ResultMessage <bool>(false, _errorHandler.Handle(ex))); } catch (Exception ex) { return(new ResultMessage <bool>(false, _errorHandler.Handle(ex))); } }
/// <summary> /// With this method, specific student has option to rate specific study material. /// This method uses transactions. /// </summary> /// <param name="studyMaterialId">Unique identifier for the study material</param> /// <param name="authorId">Unique identifier for the author</param> /// <param name="studentId">Unique identifier for the student that rates material</param> /// <param name="rating">Given rating</param> /// <returns></returns> public async Task <ResultMessage <bool> > Rate(int studyMaterialId, int studentId, int authorId, int rating) { using (IDbContextTransaction transaction = await _context.Database.BeginTransactionAsync()) { StudyMaterialRating smr = await _context.StudyMaterialRatings.FirstOrDefaultAsync(x => x.StudyMaterialId == studyMaterialId && x.StudentId == studentId); if (smr == null) { await _context.StudyMaterialRatings.AddAsync(smr = new StudyMaterialRating() { StudentId = studentId, StudyMaterialId = studyMaterialId, Rating = rating }); } else { smr.Rating = rating; } try { await _context.SaveChangesAsync(); double totalRatingForMaterial = await _context.StudyMaterialRatings .Where(x => x.StudyMaterialId == studyMaterialId) .AverageAsync(x => x.Rating); ResultMessage <bool> updatedMaterial = await _studyMaterialService.UpdateRating(studyMaterialId, totalRatingForMaterial); if (!updatedMaterial.IsSuccess) { return(new ResultMessage <bool>(updatedMaterial.Status, updatedMaterial.Message)); } double totalRatingForAuthor = await _context.StudyMaterialRatings .Where(x => x.StudyMaterial.StudentId == authorId) .AverageAsync(x => x.Rating); ResultMessage <bool> updatedStudent = await _studentService.UpdateRating(authorId, totalRatingForAuthor); if (!updatedStudent.IsSuccess) { return(new ResultMessage <bool>(updatedStudent.Status, updatedStudent.Message)); } transaction.Commit(); return(new ResultMessage <bool>(true, OperationStatus.Success)); } catch (DbUpdateException ex) { transaction.Rollback(); return(new ResultMessage <bool>(false, _errorHandler.Handle(ex))); } } }