@@ -241,6 +241,151 @@ private int Update(ActivityEntity activityEntity)
241241 return 1 ;
242242 }
243243
244+ public bool EnrollUserInActivity ( int activityId , int userId )
245+ {
246+ try
247+ {
248+ var activity = _unitOfWork . ActivityRepository . GetAllIncludeEnrollments ( activityId )
249+ . FirstOrDefault ( a => a . Id == activityId ) ;
250+ var user = _unitOfWork . UserRepository . GetById ( userId ) ;
251+
252+ if ( activity == null || user == null )
253+ return false ;
254+
255+ // Check if user is already enrolled
256+ if ( activity . UsersPlay != null && activity . UsersPlay . Any ( u => u . Id == userId ) )
257+ return false ;
258+
259+ // Check if user is already in waiting list
260+ if ( activity . UsersWaitingList != null && activity . UsersWaitingList . Any ( u => u . Id == userId ) )
261+ return false ;
262+
263+ // Check if there are available places
264+ if ( activity . UsersPlay == null || activity . UsersPlay . Count < activity . Places )
265+ {
266+ // Enroll directly
267+ if ( activity . UsersPlay == null )
268+ activity . UsersPlay = new List < UserEntity > ( ) ;
269+
270+ activity . UsersPlay . Add ( user ) ;
271+ }
272+ else
273+ {
274+ // Add to waiting list
275+ if ( activity . UsersWaitingList == null )
276+ activity . UsersWaitingList = new List < UserEntity > ( ) ;
277+
278+ activity . UsersWaitingList . Add ( user ) ;
279+ }
280+
281+ _unitOfWork . ActivityRepository . Update ( activity ) ;
282+ _unitOfWork . ActivityRepository . Save ( ) ;
283+ return true ;
284+ }
285+ catch ( Exception )
286+ {
287+ return false ;
288+ }
289+ }
290+
291+ public bool RemoveUserFromActivity ( int activityId , int userId )
292+ {
293+ try
294+ {
295+ var activity = _unitOfWork . ActivityRepository . GetAllIncludeEnrollments ( activityId )
296+ . FirstOrDefault ( a => a . Id == activityId ) ;
297+ var user = _unitOfWork . UserRepository . GetById ( userId ) ;
298+
299+ if ( activity == null || user == null )
300+ return false ;
301+
302+ bool wasEnrolled = false ;
303+
304+ // Remove from enrolled users
305+ if ( activity . UsersPlay != null && activity . UsersPlay . Any ( u => u . Id == userId ) )
306+ {
307+ activity . UsersPlay . Remove ( user ) ;
308+ wasEnrolled = true ;
309+ }
310+
311+ // Remove from waiting list
312+ if ( activity . UsersWaitingList != null && activity . UsersWaitingList . Any ( u => u . Id == userId ) )
313+ {
314+ activity . UsersWaitingList . Remove ( user ) ;
315+ }
316+
317+ _unitOfWork . ActivityRepository . Update ( activity ) ;
318+ _unitOfWork . ActivityRepository . Save ( ) ;
319+
320+ // If user was enrolled, promote from waiting list
321+ if ( wasEnrolled )
322+ {
323+ PromoteFromWaitingList ( activityId ) ;
324+ }
325+
326+ return true ;
327+ }
328+ catch ( Exception )
329+ {
330+ return false ;
331+ }
332+ }
333+
334+ public List < UserEntity > GetWaitingList ( int activityId )
335+ {
336+ var activity = _unitOfWork . ActivityRepository . GetAllIncludeWaitingList ( activityId )
337+ . FirstOrDefault ( a => a . Id == activityId ) ;
338+
339+ return activity ? . UsersWaitingList ?? new List < UserEntity > ( ) ;
340+ }
341+
342+ public int ? GetUserPositionInWaitingList ( int activityId , int userId )
343+ {
344+ var activity = _unitOfWork . ActivityRepository . GetAllIncludeWaitingList ( activityId )
345+ . FirstOrDefault ( a => a . Id == activityId ) ;
346+
347+ if ( activity ? . UsersWaitingList == null )
348+ return null ;
349+
350+ var position = activity . UsersWaitingList . FindIndex ( u => u . Id == userId ) ;
351+ return position >= 0 ? position + 1 : ( int ? ) null ;
352+ }
353+
354+ public bool PromoteFromWaitingList ( int activityId )
355+ {
356+ try
357+ {
358+ var activity = _unitOfWork . ActivityRepository . GetAllIncludeEnrollments ( activityId )
359+ . FirstOrDefault ( a => a . Id == activityId ) ;
360+
361+ if ( activity == null )
362+ return false ;
363+
364+ // Check if there are available places and users in waiting list
365+ if ( activity . UsersWaitingList != null && activity . UsersWaitingList . Any ( ) &&
366+ ( activity . UsersPlay == null || activity . UsersPlay . Count < activity . Places ) )
367+ {
368+ var firstWaitingUser = activity . UsersWaitingList . First ( ) ;
369+ activity . UsersWaitingList . Remove ( firstWaitingUser ) ;
370+
371+ if ( activity . UsersPlay == null )
372+ activity . UsersPlay = new List < UserEntity > ( ) ;
373+
374+ activity . UsersPlay . Add ( firstWaitingUser ) ;
375+
376+ _unitOfWork . ActivityRepository . Update ( activity ) ;
377+ _unitOfWork . ActivityRepository . Save ( ) ;
378+ return true ;
379+ }
380+
381+ return false ;
382+ }
383+ catch ( Exception )
384+ {
385+ return false ;
386+ }
387+ }
388+
244389 #endregion
245390 }
246391}
0 commit comments