Ejemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("Id,Text,CreatedAt")] Message message)
        {
            if (ModelState.IsValid)
            {
                _context.Add(message);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(message));
        }
 public async Task <login> AddUserAsync(login info)
 {
     _context.Add(info);
     try
     {
         await _context.SaveChangesAsync();
     }
     catch (System.Exception exp)
     {
         _Logger.LogError($"Erro in {nameof(AddUserAsync)}: " + exp.Message);
     }
     return(info);
 }
 public async Task <Messages> AddMsgAsync(Messages data)
 {
     _context.Add(data);
     try
     {
         await _context.SaveChangesAsync();
     }
     catch (System.Exception exp)
     {
         _Logger.LogError($"Error in {nameof(AddMsgAsync)}:" + exp.Message);
     }
     return(data);
 }
Ejemplo n.º 4
0
            public async Task <Message> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.FirstOrDefaultAsync(x => x.Id == request.UserId);

                var channel = await _context.Channels.FirstOrDefaultAsync(x => x.Id == request.ChannelId);

                var message = new Message
                {
                    Id        = request.Id,
                    Text      = request.Text,
                    User      = user,
                    Channel   = channel,
                    CreatedAt = request.CreatedAt
                };

                _context.Messages.Add(message);
                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(message);
                }

                throw new Exception("Problem saving message");
            }
Ejemplo n.º 5
0
        public async void setConnectId(string Name)
        {
            data = await _service.login.SingleOrDefaultAsync(c => c.Name == Name);

            data.ConnectionId = Context.ConnectionId;
            _service.login.Attach(data);
            _service.Entry(data).State = EntityState.Modified;
            _service.SaveChangesAsync();
        }
Ejemplo n.º 6
0
        public async Task <int> SaveFilePathToDb(string filePath)
        {
            var storedFile = new StoredFile
            {
                FilePath = filePath
            };
            await _context.StoredFiles.AddAsync(storedFile);

            await _context.SaveChangesAsync();

            return(_context.StoredFiles.Count());
        }
Ejemplo n.º 7
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.FindAsync(request.Id);

                if (user == null)
                {
                    throw new Exception("Cannot find user");
                }

                _context.Remove(user);

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes");
            }
Ejemplo n.º 8
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var channel = await _context.Channels.FindAsync(request.Id);

                if (channel == null)
                {
                    throw new Exception("Could not find Channel");
                }

                channel.Name = request.Name ?? channel.Name;

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes");
            }
Ejemplo n.º 9
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var message = await _context.Messages.FindAsync(request.Id);

                if (message == null)
                {
                    throw new Exception("Could not find message");
                }

                message.Text = request.Text ?? message.Text;

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes");
            }
Ejemplo n.º 10
0
        public async Task <IActionResult> StoreFile([FromBody] StoreFileModel model)
        {
            try
            {
                var storedFile = new StoredFile
                {
                    FilePath = model.filePath
                };
                _context.Conversations.Where(c => c.Id == model.ConversationId).FirstOrDefault()?.Files.Add(storedFile);
                await _context.SaveChangesAsync();

                _logger.LogInformation($"The File {Path.GetFileName(model.filePath)} of conversation {model.ConversationId} is stored");
                return(Ok("FileStored"));
            }
            catch (Exception e)
            {
                _logger.LogError($"The File {Path.GetFileName(model.filePath)} of conversation {model.ConversationId} couldn't be stored: {e.Message}");
                return(StatusCode(500, $"Internal server error: {e}"));
            }
        }
Ejemplo n.º 11
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var channel = new Channel
                {
                    Id        = request.Id,
                    Name      = request.Name,
                    CreatedAt = request.CreatedAt
                };

                _context.Channels.Add(channel);


                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes");
            }
Ejemplo n.º 12
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.FindAsync(request.Id);

                if (user == null)
                {
                    throw new Exception("Could not find User");
                }

                user.Name     = request.Name ?? user.Name;
                user.Email    = request.Email ?? user.Email;
                user.Password = request.Password ?? user.Password;

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes");
            }
Ejemplo n.º 13
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = new User
                {
                    Id        = request.Id,
                    Username  = request.Username,
                    CreatedOn = request.CreatedOn,
                    Email     = request.Email,
                    Name      = request.Name,
                    Avatar    = request.Avatar,
                    Password  = request.Password
                };

                _context.Users.Add(user);
                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving User");
            }
Ejemplo n.º 14
0
 public async ValueTask DisposeAsync()
 {
     await _context.SaveChangesAsync();
 }