Beispiel #1
0
        public async Task <IEnumerable <Participant> > CreateRangeAsync(ParticipantRange range, ApplicationUser user, Action <string, string> AddErrorMessage)
        {
            if (range.Users == null || range.Users.Count == 0)
            {
                return(null);
            }

            if (user == null)
            {
                AddErrorMessage?.Invoke("General", "Only Logged-in user can perform this operation");
                return(null);
            }

            Chatroom chatroom = await _chatroomService.GetAsync(range.ChatroomID);

            if (user.Id != chatroom.OwnerID)
            {
                AddErrorMessage?.Invoke("General", "Only Owner of a Chatroom can add participants!");
                return(null);
            }

            List <ParticipantEntity> participants = new List <ParticipantEntity>();

            foreach (UserShort u in range.Users)
            {
                ParticipantEntity temp = new ParticipantEntity
                {
                    ChatroomID = range.ChatroomID,
                    UserID     = u.UserID,
                    User       = new ApplicationUser {
                        Id = u.UserID, UserName = u.UserName
                    },
                };
                _context.Entry(temp.User).State = EntityState.Unchanged;
                participants.Add(temp);
            }

            IEnumerable <ParticipantEntity> created = await _repository.CreateRangeAsync(participants);

            if (created != null && created.Any())
            {
                await _context.SaveChangesAsync();
            }

            IEnumerable <Participant> result = _mappingService.EntitiesToDtos(created);

            foreach (Participant p in result)
            {
                p.Deletable = (p.UserID != user.Id);
            }

            return(result);
        }
        public async Task <IEnumerable <Participant> > CreateRange([FromBody] ParticipantRange range)
        {
            ApplicationUser user = await _userManager.GetUserAsync(HttpContext.User);

            IEnumerable <Participant> created = await _participantService.CreateRangeAsync(range, user, ModelState.AddModelError);

            if (created == null)
            {
                return(null);
            }

            return(created);
        }