-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImpCourseRepository.cs
243 lines (205 loc) · 8.58 KB
/
ImpCourseRepository.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PEP.Data;
using PEP.Models.Domain;
using PEP.Repositories.Interface;
namespace PEP.Repositories.Implement
{
public class ImpCourseRepository : ICourseRepository
{
private readonly FinalDesignContext dbContext;
public ImpCourseRepository(FinalDesignContext dbContext)
{
this.dbContext = dbContext;
}
public async Task<CourseChapter?> AddChapter(CourseChapter chapter)
{
await dbContext.CourseChapters.AddAsync(chapter);
await dbContext.SaveChangesAsync();
return chapter;
}
public async Task<Course> AddCourseAsync(Course course)
{
await dbContext.Courses.AddAsync(course);
await dbContext.SaveChangesAsync();
return course;
}
public async Task<SubChapter?> AddSubChapter(SubChapter subChapter)
{
await dbContext.SubChapters.AddAsync(subChapter);
await dbContext.SaveChangesAsync();
return subChapter;
}
public async Task<CourseChapter?> DeleteChapterById(int chapter)
{
var existingCourseChapter = await dbContext.CourseChapters.Include(cc=>cc.SubChapters).FirstOrDefaultAsync(cc => cc.ChapterId == chapter);
if (existingCourseChapter == null)
{
return null;
}
dbContext.CourseChapters.Remove(existingCourseChapter);
await dbContext.SaveChangesAsync();
return existingCourseChapter;
}
public async Task<Course?> DeleteCourseByIdAsync(int courseId)
{
var course = await dbContext.Courses
.Include(c => c.UserCourses)
.Include(c => c.CourseTags)
.Include(c => c.CourseChapters)
.ThenInclude(cc => cc.SubChapters)
.FirstOrDefaultAsync(c => c.CourseId == courseId);
if (course == null)
{
return null;
}
dbContext.Courses.Remove(course);
await dbContext.SaveChangesAsync();
return course;
}
public async Task<SubChapter?> DeleteSubChapterById(int subChapterId)
{
var subChapter = await dbContext.SubChapters.FirstOrDefaultAsync(s => s.SubChapterId == subChapterId);
if (subChapter == null)
{
return null;
}
dbContext.SubChapters.Remove(subChapter);
await dbContext.SaveChangesAsync();
return subChapter;
}
public async Task<List<Course>> GetAllCoursesListAsync(string? fitlerQuery, int pageNumber, int? pageSize)
{
var allQueryCourses = dbContext.Courses.Include(c => c.CourseTags).AsQueryable();
if (!string.IsNullOrEmpty(fitlerQuery))
{
fitlerQuery = fitlerQuery.Trim();
allQueryCourses = allQueryCourses.Where(c => c.CourseName.Contains(fitlerQuery));
}
if (pageSize == null)
{
return await allQueryCourses.ToListAsync();
}
else
{
int skipResult = (pageNumber - 1) * pageSize.Value;
return await allQueryCourses.Skip(skipResult).Take(pageSize.Value).ToListAsync();
}
}
public async Task<CourseChapter?> GetChapterById(int chapterId)
{
var existingCourseChapter =await dbContext.CourseChapters.FirstOrDefaultAsync(cc => cc.ChapterId == chapterId);
if (existingCourseChapter == null)
{
return null;
}
return existingCourseChapter;
}
public async Task<Course?> GetCourseByIdAsync(int courseId)
{
var course = await dbContext.Courses
.Include(c => c.CourseTags)
.Include(c => c.CourseChapters)
.ThenInclude(cc => cc.SubChapters)
.FirstOrDefaultAsync(c => c.CourseId == courseId);
if (course == null)
return null;
return course;
}
public async Task<SubChapter?> GetSubChapterById(int subChapterId)
{
var subChapter = await dbContext.SubChapters.FirstOrDefaultAsync(sc => sc.SubChapterId == subChapterId);
if (subChapter == null)
{
return null;
}
return subChapter;
}
public Task<List<Course>> GetUserCoursesListAsync(int userId)
{
throw new NotImplementedException();
}
public async Task<CourseChapter?> UpdateChapter(int chapterId, CourseChapter chapter)
{
var existingCourseChapter =await dbContext.CourseChapters.Include(cc=>cc.SubChapters).FirstOrDefaultAsync(cc=>cc.ChapterId== chapterId);
if (existingCourseChapter == null)
{
return null;
}
existingCourseChapter.ChapterNumber = chapter.ChapterNumber;
existingCourseChapter.Title = chapter.Title;
existingCourseChapter.SubChapters = chapter.SubChapters;
await dbContext.SaveChangesAsync();
return existingCourseChapter;
}
public async Task<Course?> UpdateCourseStepOneAsync(int courseId, Course course)
{
var existingCourse = await dbContext.Courses.Include(c => c.CourseTags).FirstOrDefaultAsync(c => c.CourseId == courseId);
if (existingCourse == null)
{
return null;
}
existingCourse.CourseName = course.CourseName;
existingCourse.ChapterCount = course.ChapterCount;
existingCourse.ImageUrl = course.ImageUrl;
existingCourse.Introduction = course.Introduction;
existingCourse.CourseTags = course.CourseTags;
await dbContext.SaveChangesAsync();
return existingCourse;
}
public async Task<Course?> UpdateCourseStepTwoAsync(int courseId, Course updatedCourse)
{
var existingCourse = await dbContext.Courses
.Include(c => c.CourseChapters)
.ThenInclude(cc => cc.SubChapters)
.FirstOrDefaultAsync(c => c.CourseId == courseId);
if (existingCourse == null)
{
return null;
}
// 遍历更新 CourseChapter 的 title 字段
foreach (var updatedChapter in updatedCourse.CourseChapters)
{
var existingChapter = existingCourse.CourseChapters.FirstOrDefault(cc => cc.ChapterId == updatedChapter.ChapterId);
if (existingChapter != null)
{
existingChapter.Title = updatedChapter.Title;
// 遍历更新 SubChapter 的 title 字段
foreach (var updatedSubChapter in updatedChapter.SubChapters)
{
var existingSubChapter = existingChapter.SubChapters.FirstOrDefault(sc => sc.SubChapterId == updatedSubChapter.SubChapterId);
if (existingSubChapter != null)
{
existingSubChapter.Title = updatedSubChapter.Title;
}
}
}
}
await dbContext.SaveChangesAsync();
return existingCourse;
}
public async Task<SubChapter?> UpdateSubChapter(int subChapterId, SubChapter subChapter)
{
var exsitingSubChapter = await dbContext.SubChapters.FirstOrDefaultAsync(c => c.SubChapterId == subChapterId);
if (exsitingSubChapter == null)
{
return null;
}
exsitingSubChapter.SubChapterNumber = subChapter.SubChapterNumber;
exsitingSubChapter.Title = subChapter.Title;
exsitingSubChapter.MarkdownContent = subChapter.MarkdownContent;
await dbContext.SaveChangesAsync();
return exsitingSubChapter;
}
public async Task<SubChapter?> UpdateSubChapterMDcontent(int subChapterId, SubChapter subChapter)
{
var existingSubChapter = await dbContext.SubChapters.FirstOrDefaultAsync(c => c.SubChapterId == subChapterId);
if (existingSubChapter == null)
return null;
existingSubChapter.MarkdownContent = subChapter.MarkdownContent;
await dbContext.SaveChangesAsync();
return existingSubChapter;
}
}
}