Exemplo n.º 1
0
        public async Task <IActionResult> Create([FromBody] SessionViewModel sessionViewModel)
        {
            try
            {
                var session = new Session
                {
                    Capacity = sessionViewModel.Capacity,
                    Name     = sessionViewModel.Name
                };

                var shortSessionCode = session.Id.ToString().Replace("-", "").Substring(0, 8).ToUpper();
                session.ShortSessionCode = shortSessionCode;

                var user = await _usersRepository.FindUser(sessionViewModel.UserId);

                if (user != null)
                {
                    await _sessionsRepository.CreateSession(session);

                    await _sessionsRepository.AddUserToSession(user, session);

                    return(Ok(session));
                }
                else
                {
                    return(BadRequest(new ErrorMessage("Unable to create session")));
                }
            }
            catch (Exception ex)
            {
                return(HandleError(ex));
            }
        }