Beispiel #1
0
        [HttpPost("createChat")]//now work work yet
        public IActionResult CreateChat(int id, string name, string type)
        {
            IActionResult result       = new ObjectResult(false);
            GenericResult createResult = null;

            var authenticationHeader = Request?.Headers["Authorization"];
            var token   = authenticationHeader?.FirstOrDefault().Split(' ')[1];
            var subject = _jwtFormater.GetSubject(token);

            var user = _userRepository.GetSingleByUsername(subject);

            try
            {
                var chat = new Chat()
                {
                    Name        = name,
                    DateCreated = DateTime.Now.ToString(),
                    Type        = type
                };
                _chatRepository.Add(chat);
                _chatRepository.Commit();

                var cu = new ChatUser()
                {
                    ChatId = chat.Id,
                    UserId = user.Id
                };
                var cu1 = new ChatUser()
                {
                    ChatId = chat.Id,
                    UserId = id
                };
                _chatUserRepository.Add(cu);
                _chatUserRepository.Add(cu1);
                _chatUserRepository.Commit();

                createResult = new GenericResult()
                {
                    Succeeded = true,
                    Message   = chat.Id + ""
                };
            }
            catch (Exception e)
            {
                createResult = new GenericResult()
                {
                    Succeeded = false,
                    Message   = e.Message
                };
            }

            result = new ObjectResult(createResult);
            return(result);
        }