public async Task <ResponseModel> UnlockContent(int currentContentNo, int levelNo, Guid traineeId) { int serialNumber = currentContentNo + 1; /* * is the content is allowed to be unlocked by this user? * did the user finished the previous content/quiz? skip if the previous is 0 * insert new history entry */ Level currentLevel = await Db.Levels.FirstOrDefaultAsync(x => x.No == levelNo); Content nextContent = await Db.Contents.FirstOrDefaultAsync(x => x.No == serialNumber && x.LevelId == currentLevel.Id); ResponseModel response = await validationService.IsValidRequestAsync(nextContent.Id, traineeId); if (response.IsSuccess) { if (currentContentNo > 0) { Content currentContent = await Db.Contents.FirstOrDefaultAsync(x => x.No == currentContentNo && x.LevelId == currentLevel.Id); TraineeQuizHistory prevHistory = await Db.TraineeQuizHistories.FirstOrDefaultAsync( x => x.ContentId == currentContent.Id && x.TraineeId == traineeId); if (!prevHistory.IsCompleted) { response = new ResponseModel(null, false, MessageContainer.PreviousContentIsUncompleted); } } if (response.IsSuccess) { var history = new TraineeHistory() { ContentId = nextContent.Id, TraineeId = traineeId, LastAccessed = DateTime.Now, Unlocked = DateTime.Now, Point = 0, }; Db.TraineeHistories.Add(history); await Db.SaveChangesAsync(); response = new ResponseModel(new TraineeHistory() { Id = history.Id, ContentId = history.ContentId, TraineeId = history.TraineeId }); } } return(response); }
public async Task <bool> IsUnlocked(Guid contentId, Guid traineeId) { TraineeHistory history = await Db.TraineeHistories.FirstOrDefaultAsync(x => x.ContentId == contentId && x.TraineeId == traineeId); return(history != null); }