Exemplo n.º 1
0
        public async Task <DeleteScheduleSessionRs> Delete(int id)
        {
            var rs = new DeleteScheduleSessionRs();

            var schedule = await _dbContext.Schedules.FirstOrDefaultAsync(s => s.Id == id);

            if (schedule == null)
            {
                throw new Exception("Lịch học không tồn tại");
            }

            var userAndCountRegistrationMap = schedule.ScheduleDetails
                                              .SelectMany(x => x.Registrations)
                                              .Select(x => x.UserId)
                                              .GroupBy(x => x)
                                              .ToDictionary(x => x.Key, x => x.Count());

            rs.IsUserGetSessionBack = ReturnSessionBackToRegisteredUser(userAndCountRegistrationMap);

            _dbContext.Schedules.Remove(schedule);
            await _dbContext.SaveChangesAsync();

            rs.Success = true;
            return(rs);
        }
Exemplo n.º 2
0
        public async Task <DeleteScheduleSessionRs> DeleteSession(int scheduleDetailId)
        {
            var session = await _dbContext.ScheduleDetails.FirstOrDefaultAsync(x => x.Id == scheduleDetailId);

            if (session == null)
            {
                throw new Exception("Buổi học không tồn tại!");
            }

            if (session.SessionNo == 1)
            {
                return(await this.Delete(session.ScheduleId));
            }

            var rs = new DeleteScheduleSessionRs();

            var schedule = await _dbContext.Schedules.FirstOrDefaultAsync(s => s.Id == session.ScheduleId);

            schedule.Sessions = session.SessionNo - 1;

            var deletedSessions = _dbContext.ScheduleDetails
                                  .Where(x => x.ScheduleId == session.ScheduleId && x.SessionNo >= session.SessionNo);

            var userAndCountRegistrationMap = await deletedSessions
                                              .SelectMany(x => x.Registrations)
                                              .Select(x => x.UserId)
                                              .GroupBy(x => x)
                                              .ToDictionaryAsync(x => x.Key, x => x.Count());

            rs.IsUserGetSessionBack = ReturnSessionBackToRegisteredUser(userAndCountRegistrationMap);

            _dbContext.ScheduleDetails.RemoveRange(deletedSessions);
            await _dbContext.SaveChangesAsync();

            rs.Success = true;
            return(rs);
        }