Ejemplo n.º 1
0
        public async Task <int> Handle(CreateMessageCommand request, CancellationToken cancellationToken)
        {
            var user = _context.Individuals.Include(x => x.Roles).ThenInclude(r => r.Role).FirstOrDefault(x => x.Id == request.SentById);

            if (user == null || !user.CanSendMessage())
            {
                throw new Exception("User has not been authorized to make this request");
            }

            var entity = new Message
            {
                Regarding = _context.Students.FirstOrDefault(x => x.Id == request.RegardingId),
                SentBy    = _context.Individuals.FirstOrDefault(x => x.Id == request.SentById),
                SentTo    = _context.Individuals.FirstOrDefault(x => x.Id == request.SentToId),
                SentDate  = DateTime.Now,
                Type      = _context.MessageTypes.FirstOrDefault(x => x.Id == request.TypeId),
                Text      = request.Text
            };

            _context.Messages.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(entity.Id);
        }
Ejemplo n.º 2
0
        public async Task<int> Handle(CreatePublicNotificationCommand request, CancellationToken cancellationToken)
        {
            //Verificar si el usuario tiene algun rol que permita enviar notificaciones (eg. es docente). Reemplazar por auth.
            var user = _context.Individuals.Include(x => x.Roles).ThenInclude(r => r.Role).FirstOrDefault(x => x.Id == request.PersonId);
            if (user == null || !user.CanSendNotification())
                throw new Exception("User has not been authorized to make this request");

            var entity = new Notification
            {
                SentDate = DateTime.Now,
                Title = request.Title,
                Text = request.Text,
                Type = Domain.Enumerations.NotificationTypeEnum.Public
            };

            //Agregar mensaje a cada individuo
            var individuals = _context.Individuals.Select(x => x.Id).ToList();
            foreach (var individual in individuals)
            {
                var personNotif = new PersonNotification()
                {
                    ReceiverId = individual,
                    NotificationId = entity.Id
                };

                entity.IndividualNotifications.Add(personNotif);
            }
            _context.Notifications.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return entity.Id;
        }
Ejemplo n.º 3
0
        public async Task <Unit> Handle(DeleteGroupCommand request, CancellationToken cancellationToken)
        {
            var entity = await _context.Groups.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(Group), request.Id);
            }

            _context.Groups.Remove(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Ejemplo n.º 4
0
        public async Task <int> Handle(CreateGroupCommand request, CancellationToken cancellationToken)
        {
            var entity = new Group
            {
                Description = request.Description,
                Year        = request.Year,
                Section     = request.Section,
                TimeSpan    = request.TimeSpan,
                Active      = true
            };

            _context.Groups.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(entity.Id);
        }
Ejemplo n.º 5
0
        public async Task <Unit> Handle(UpdateGroupCommand request, CancellationToken cancellationToken)
        {
            var entity = await _context.Groups.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(Group), request.Id);
            }

            entity.Id          = request.Id;
            entity.Description = request.Description;
            entity.Year        = request.Year;
            entity.Section     = request.Section;
            entity.TimeSpan    = request.TimeSpan;
            entity.Active      = request.Active;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }