public async Task <CreateSessionResponseModel> CreateSession([FromBody] CreateSessionRequestModel model)
 {
     try
     {
         var blCreateGame = new BL_CreateGame();
         return(await blCreateGame.CreationSessionAsync(model));
     }
     catch (Exception ex)
     {
         ApplicationFactory.CurrentLogger.Error($"CreateGameController > CreateSession error. {ex.Message}");
         throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
     }
 }
        public async Task <IActionResult> Create([FromBody] CreateSessionRequestModel requestModel)
        {
            var accountId = (int)HttpContext.Items["accountId"];

            var msg = "";

            if (accountId < 1)
            {
                return(Unauthorized(new ErrorResponseModel("You're not authorized")));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    bool ownsClient = await _clientService.DoesPractitionerOwnsClient(accountId, requestModel.ClientId);

                    if (!ownsClient)
                    {
                        msg = "You cannot access client with id " + requestModel.ClientId;

                        return(StatusCode(StatusCodes.Status403Forbidden, new ErrorResponseModel(msg)));
                    }

                    var inProgressSessions = await _sessionService.GetSessionsInProgressAsync(requestModel.ClientId, accountId);

                    var sessionsCount = inProgressSessions.Count;
                    if (sessionsCount > 0)
                    {
                        msg = $"There are {sessionsCount} in progress sessions. " +
                              "Please finish all older sessions in order to create a new one.";

                        return(StatusCode(StatusCodes.Status403Forbidden, new ErrorResponseModel(msg)));
                    }

                    var createSession = _mapper.Map <CreateSession>(requestModel);

                    var session = await _bodyImageBusinessService.SaveBodyImageSetAsync(accountId, createSession);

                    var sessionResponse = _mapper.Map <SessionResponseModel>(session);
                    return(Created("", sessionResponse));
                }
                catch (Exception e)
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError,
                                      new ErrorResponseModel("Application error", e.Message, ModelState)));
                }
            }

            return(BadRequest(new ErrorResponseModel(ModelState)));
        }
Beispiel #3
0
        public CreateSessionResponseModel CreateSession(CreateSessionRequestModel model)
        {
            try
            {
                ApplicationFactory.LogWithObject($@"BL_CreateGame > CreateSession start.", model);

                ApplicationFactory.ValidateRequestModel(new CreateSessionRequestModelValidator(), model);

                //refactor using validator
                if (model.Name.Length > 10)
                {
                    //result.Message = "Player name has maximum length of 10 characters.";
                    //throw new Exception(result.Message);
                }
            }
            catch (Exception ex)
            {
                ApplicationFactory.CurrentLogger.Error($"BL_CreateGame > CreateSession error. {ex.Message}");
                //return structured response
            }

            CreateSessionResponseModel result = new CreateSessionResponseModel()
            {
                SessionPin = "0"
            };

            try
            {
                var sessionPin  = GenerateRandomNo().ToString();
                var currentTime = DateTime.Now;

                T_Game_Sessions newGame = new T_Game_Sessions()
                {
                    SessionPin      = sessionPin,
                    CreateTime      = currentTime,
                    SessionIsActive = true
                };

                db.T_Game_Sessions.Add(newGame);
                db.SaveChanges();

                T_Game_Session_Players gameLeader = new T_Game_Session_Players()
                {
                    SessionId    = newGame.SessionId,
                    PlayerName   = model.Name,
                    JoinTime     = currentTime,
                    DeviceId     = model.DeviceId,
                    RequestAppId = model.AppId,
                };

                db.T_Game_Session_Players.Add(gameLeader);
                db.SaveChanges();

                result.SessionPin = sessionPin;
            }
            catch (Exception ex)
            {
                ApplicationFactory.CurrentLogger.Error($"BL_CreateGame > CreateSession error. {ex.Message}");
                //return structured response
            }

            ApplicationFactory.LogWithObject($"BL_CreateGame > CreateSession success.", result);
            return(result);
        }
Beispiel #4
0
        // add constructors in future if needed to read header auth tokens

        #region CreateSession
        public async Task <CreateSessionResponseModel> CreationSessionAsync(CreateSessionRequestModel model)
        {
            return(await Task.Run(() => CreateSession(model)));
        }