public async Task Put(int chapter, int exercise, [FromBody] string savedWork) { var dataToSave = new SavedWork { UserId = userService.GetCurrentUserId(), ChapterNo = chapter, ExerciseNo = exercise, SavedCode = savedWork ?? "" }; await repository.SaveWorkAsync(dataToSave); }
// Saved work public async Task SaveWorkAsync(SavedWork savedWork) { var existingData = context.SavedWork.Where(s => s.UserId == savedWork.UserId && s.ChapterNo == savedWork.ChapterNo && s.ExerciseNo == savedWork.ExerciseNo); if (await existingData.AnyAsync()) { savedWork.SavedWorkId = existingData.Single().SavedWorkId; context.Entry(existingData.Single()).CurrentValues.SetValues(savedWork); await context.SaveChangesAsync(); } else { context.SavedWork.Add(savedWork); await context.SaveChangesAsync(); } }