Exemplo n.º 1
0
        public async Task <IActionResult> Create(CreateModel model)
        {
            Chat chat = repos1.GetOnGUID(model.Name);

            byte[] post = null;
            if (model.File != null)
            {
                using (var binaryReader = new BinaryReader(model.File.OpenReadStream()))
                {
                    post = binaryReader.ReadBytes((int)model.File.Length);
                }
            }
            User    user    = repos.Get(Convert.ToInt32(model.Email));
            Message message = new Message {
                Color = 0, ChatId = chat.Id, Text = model.Text, UserId = user.UserId
            };

            if (this.User.FindFirstValue(ClaimTypes.Role) == "Admin")
            {
                message.Color = 1;
            }
            string login = user.UserName;

            if (model.File == null)
            {
                repos3.Create(message);
            }
            else
            {
                Document doc = new Document {
                    GUID = Guid.NewGuid().ToString(), Name = model.File.FileName, Data = post
                };
                repos3.CreateWithDoc(message, doc);
            }
            message.Doc = repos3.GetDoc(message.DocId);
            log.Info("Created new chat " + '\n'
                     + "Chat name: " + chat.Name + '\n'
                     + "Chat creator: " + model.Name);
            await hubContext.Clients.Group(chat.GUID).SendAsync("ReceiveMessage", login, message);

            return(RedirectToAction("Chat", "Chat", new { chat.GUID }));
        }