1717
1818namespace NXTBackend . API . Core . Services . Implementation ;
1919
20- public sealed class GoalService ( DatabaseContext ctx ) : BaseService < LearningGoal > ( ctx ) , IGoalService
20+ public sealed class GoalService : BaseService < LearningGoal > , IGoalService
2121{
22+ public GoalService ( DatabaseContext ctx ) : base ( ctx )
23+ {
24+ DefineFilter < Guid > ( "id" , ( q , id ) => q . Where ( g => g . Id == id ) ) ;
25+ DefineFilter < string > ( "name" , ( q , name ) => q . Where ( g => EF . Functions . Like ( g . Name , $ "%{ name } %") ) ) ;
26+ DefineFilter < string > ( "slug" , ( q , slug ) => q . Where ( g => EF . Functions . Like ( g . Slug , $ "%{ slug } %") ) ) ;
27+ }
28+
2229 /// <inheritdoc />
2330 public async Task < ( LearningGoal ? , User ? ) > IsCollaborator ( Guid entityId , Guid userId )
2431 {
@@ -46,7 +53,7 @@ public async Task<bool> AddCollaborator(Guid goalId, Guid userId)
4653 user = await _context . Users . FindAsync ( userId ) ;
4754 if ( user is null )
4855 throw new ServiceException ( StatusCodes . Status404NotFound , "User not found" ) ;
49-
56+
5057 await _context . CursusCollaborator . AddAsync ( new ( )
5158 {
5259 CursusId = goal . Id ,
@@ -62,7 +69,7 @@ public async Task<bool> RemoveCollaborator(Guid goalId, Guid userId)
6269 . AsNoTracking ( )
6370 . Where ( e => e . GoalId == goalId && e . UserId == userId )
6471 . FirstOrDefaultAsync ( ) ;
65-
72+
6673 if ( result is null )
6774 return false ;
6875
@@ -95,7 +102,7 @@ public async Task<IEnumerable<Project>> GetProjects(LearningGoal goal, SortingPa
95102 var projects = _context . GoalProject . AsNoTracking ( )
96103 . Where ( gp => gp . GoalId == goal . Id )
97104 . Select ( gp => gp . Project ) ;
98-
105+
99106 return await SortedList < Project > . Apply ( projects , sorting ) . ToListAsync ( ) ;
100107 }
101108
@@ -105,28 +112,28 @@ public async Task<IEnumerable<Project>> SetProjects(LearningGoal goal, IEnumerab
105112 var projectIds = projects . ToList ( ) ;
106113 if ( projectIds . Count > 4 )
107114 throw new ServiceException ( StatusCodes . Status422UnprocessableEntity , "Maximum number of projects exceeded" ) ;
108-
115+
109116 var existingProjects = await _context . Projects
110117 . Where ( p => projectIds . Contains ( p . Id ) )
111118 . ToListAsync ( ) ;
112-
119+
113120 if ( existingProjects . Count != projectIds . Count )
114121 throw new ServiceException ( StatusCodes . Status422UnprocessableEntity , "Non-existent projects" ) ;
115122
116123 var existingRelations = await _context . GoalProject
117124 . Where ( gp => gp . GoalId == goal . Id && projectIds . Contains ( gp . ProjectId ) )
118125 . Select ( gp => gp . ProjectId )
119126 . ToListAsync ( ) ;
120-
127+
121128 if ( existingRelations . Count is not 0 )
122129 throw new ServiceException ( StatusCodes . Status409Conflict , "Some projects were already associated with this goal" ) ;
123-
130+
124131 var goalProjects = projectIds . Select ( projectId => new GoalProject
125132 {
126133 GoalId = goal . Id ,
127134 ProjectId = projectId ,
128135 } ) ;
129-
136+
130137 await _context . GoalProject . AddRangeAsync ( goalProjects ) ;
131138 await _context . SaveChangesAsync ( ) ;
132139 return existingProjects ;
0 commit comments