public async Task <IActionResult> SetPosition(Guid id, string email, int x, int y) { var gameSession = await _gameSessionService.GetGameSession(id); await _gameSessionService.AddTurn(gameSession.Id, email, x, y); return(View("Index", gameSession)); }
public async Task <IActionResult> SetPosition([FromRoute] Guid sessionId) { if (sessionId != Guid.Empty) { using (var reader = new StreamReader(Request.Body, Encoding.UTF8, true, 1024, true)) { var bodyString = await reader.ReadToEndAsync(); if (string.IsNullOrEmpty(bodyString)) { return(BadRequest("Body is empty")); } var turn = JsonConvert.DeserializeObject <TurnModel>(bodyString); if (turn == null) { return(BadRequest("You must pass a TurnModel object in your body")); } turn.User = await HttpContext.RequestServices.GetService <IUserService>().GetUserByEmail(turn.Email); turn.UserId = turn.User.Id; var gameSession = await _gameSessionService.GetGameSession(sessionId); if (gameSession == null) { return(BadRequest($"Cannot find Game Session {sessionId}")); } if (gameSession.ActiveUser.Email != turn.User.Email) { return(BadRequest($"{turn.User.Email} cannot play this turn")); } gameSession = await _gameSessionService.AddTurn(gameSession.Id, turn.User, turn.X, turn.Y); if (gameSession != null && gameSession.ActiveUser.Email != turn.User.Email) { return(Ok(gameSession)); } else { return(BadRequest("Cannot save turn")); } } } return(BadRequest("Id is empty")); }
[HttpPost("/restapi/v1/SetGamePosition/{sessionId}")] // Metoda pobiera wartość elementu do wykonania z treści żądania HTTP. public async Task <IActionResult> SetPosition([FromRoute] Guid sessionId) //Określa, że parametr lub właściwość należy powiązać przy użyciu danych trasy z bieżącego żądania { if (sessionId != Guid.Empty) { using (var reader = new StreamReader(Request.Body, Encoding.UTF8, true, 1024, true)) // zczytanie dancyh przesłanych przez "/restapi/v1/SetGamePosition/{sessionId}" ///Zapewnia wygodną składnię, która zapewnia poprawne korzystanie z { var bodyString = reader.ReadToEnd(); // zczytanie wszystkich danych/ if (string.IsNullOrEmpty(bodyString)) // sprawdzenie czy bodystring nie jest null { return(BadRequest("Treść jest pusta")); } var turn = JsonConvert.DeserializeObject <TurnModel>(bodyString); //Deserializes the JSON to a .NET object. turn.User = await HttpContext.RequestServices.GetService <IUserService>().GetUserByEmail(turn.Email); //Gets or sets the IServiceProvider that provides access to the request's service container turn.UserId = turn.User.Id; if (turn == null) // sprarwdzenie czy turn jest nullem { return(BadRequest("W treści należy przesłać obiekt TurnModel")); } var gameSession = await _gameSessionService.GetGameSession(sessionId); // pobranie gameSession z ConcurentBag'a if (gameSession == null) // sprawdzenie czy sesja istnieje { return(BadRequest($"Nie można znaleźć rozgrywki {sessionId}")); } if (gameSession.ActiveUser.Email != turn.User.Email) // zabezpieczenie przez nieporządanym ruchem { return(BadRequest($"{turn.User.Email} nie ma w tej chwili ruchu w grze")); } gameSession = await _gameSessionService.AddTurn(gameSession.Id, turn.User.Email, turn.X, turn.Y); // dodanie tury do concurent bag'a if (gameSession != null && gameSession.ActiveUser.Email != turn.User.Email) // jeżeli wszystko jest ok zwróć OK { return(Ok(gameSession)); } else { return(BadRequest("Nie można zapisać ruchu")); // Bad request w przypadku braku zapisania } } } return(BadRequest("Identyfikator Id jest pusty")); // badrequest w przypadku złego id }
public async Task <IActionResult> SetPosition([FromRoute] Guid sessionId) { if (sessionId != Guid.Empty) { using (var reader = new StreamReader(Request.Body, Encoding.UTF8, true, 1024, true)) { var bodyString = reader.ReadToEnd(); if (string.IsNullOrEmpty(bodyString)) { return(BadRequest("Treść jest pusta")); } var turn = JsonConvert.DeserializeObject <TurnModel>(bodyString); turn.User = await HttpContext.RequestServices.GetService <IUserService>().GetUserByEmail(turn.Email); turn.UserId = turn.User.Id; if (turn == null) { return(BadRequest("W treści należy przesłać obiekt TurnModel")); } var gameSession = await _gameSessionService.GetGameSession(sessionId); if (gameSession == null) { return(BadRequest($"Nie można znaleźć rozgrywki {sessionId}")); } if (gameSession.ActiveUser.Email != turn.User.Email) { return(BadRequest($"{turn.User.Email} nie ma w tej chwili ruchu w grze")); } gameSession = await _gameSessionService.AddTurn(gameSession.Id, turn.User, turn.X, turn.Y); if (gameSession != null && gameSession.ActiveUser.Email != turn.User.Email) { return(Ok(gameSession)); } else { return(BadRequest("Nie można zapisać ruchu")); } } } return(BadRequest("Identyfikator Id jest pusty")); }