示例#1
0
        public async Task <IActionResult> Join(string id)
        {
            if (User.FindFirst(ClaimTypes.NameIdentifier) == null)
            {
                return(BadRequest());
            }

            var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;


            var user = await _context.ChannelUsers.Where(x => x.UserId == userId && x.ChannelId == id).FirstOrDefaultAsync();

            if (user != null)
            {
                return(BadRequest());
            }

            var channelUser = new ChannelUser()
            {
                ChannelId = id,
                UserId    = userId
            };

            _context.ChannelUsers.Add(channelUser);
            await _context.SaveChangesAsync();

            var channel = await _context.Channels.Include(x => x.ChannelUsers).ThenInclude(y => y.User).Include(x => x.Messages).ThenInclude(y => y.Sender).FirstOrDefaultAsync(x => x.Id == id);

            return(Ok(channelUser));
        }
示例#2
0
        public async Task NewMessage(ChannelUser msg)
        {
            ChannelUser mesaj = new ChannelUser();

            msg.Id = Guid.NewGuid().ToString();
            try
            {
                var user = await _context.ChannelUsers.Where(x => x.UserId == msg.UserId && x.ChannelId == msg.ChannelId).FirstOrDefaultAsync();

                if (user == null)
                {
                    _context.ChannelUsers.Add(msg);
                    await _context.SaveChangesAsync();

                    mesaj = await _context.ChannelUsers.Include(x => x.Channel).Include(x => x.User).FirstOrDefaultAsync(x => x.Id == msg.Id);
                }
            }
            catch (Exception ex)
            {
            }

            if (Clients != null)
            {
                await Clients.All.SendAsync("MessageReceived", mesaj);
            }
        }
示例#3
0
        public async Task <User> Register(User u, string password)
        {
            var user = await _context.Users.FirstOrDefaultAsync(x => x.Email == u.Email);

            if (user != null)
            {
                throw new Exception("User already exists");
            }

            try
            {
                byte[] passwordHash, passwordSalt;
                HelperMethods.CreateHash(password, out passwordHash, out passwordSalt);
                u.PasswordHash = passwordHash;
                u.PasswordSalt = passwordSalt;

                await _context.Users.AddAsync(u);

                var result = await _context.SaveChangesAsync();

                if (result > 0)
                {
                    return(u);
                }
                throw new Exception("Database error!");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#4
0
        public async Task NewMessage(MessageCreateDto msg)
        {
            var mesaj = new Message();

            try
            {
                mesaj.CreateMessage(msg);
                _context.Messages.Add(mesaj);
                await _context.SaveChangesAsync();

                mesaj = await _context.Messages.Include(x => x.Sender).FirstOrDefaultAsync(x => x.Id == mesaj.Id);
            }
            catch (Exception ex)
            {
            }

            if (Clients != null)
            {
                await Clients.All.SendAsync("MessageReceived", mesaj);
            }
        }