Skip to content

Commit

Permalink
Nuevo EndPoint para la obtensión de la partida pendiente de terminar(…
Browse files Browse the repository at this point in the history
…GetParty). Se cambió el parámetro para los EndPoint NewGame(ahora se recibe el nickname del usuario).
  • Loading branch information
MatiasMolina000 committed Mar 2, 2023
1 parent 0917bfc commit a2e0d77
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 158 deletions.
11 changes: 9 additions & 2 deletions JuegoBingoAPI/Controllers/BingoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace JuegoBingoAPI.Controllers
public class BingoController : ControllerBase
{
[HttpPost("NewGame")]
public ResponseModel NewGame(string usuarioId)
public ResponseModel NewGame(string usuarioName)
{
var rule = new BingoRule();
return rule.NewGame(usuarioId);
return rule.NewGame(usuarioName);
}

[HttpPost("NewNumber")]
Expand All @@ -22,5 +22,12 @@ public ResponseModel NewNumber(string partidaId)
var rule = new BingoRule();
return rule.NewNumber(partidaId);
}

[HttpGet("GetParty")]
public ResponseModel GetParty(string usuarioName)
{
var rule = new BingoRule();
return rule.GetParty(usuarioName);
}
}
}
74 changes: 37 additions & 37 deletions JuegoBingoAPI/Data/BingoData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,40 @@ namespace JuegoBingoAPI.Data
{
public class BingoData
{
public string GetUserIdByName(string userName) {

using var cnn = new SqlConnection(new ConnectionDB().ConnectionStringSQL());

string query = $"SELECT ID FROM AspNetUsers WITH(NOLOCK) WHERE UserName = '{userName}'";

var response = cnn.QueryFirstOrDefault<string>(query);

return response;
}

public string GetPartidaByUserName(string userName)
{
using var cnn = new SqlConnection(new ConnectionDB().ConnectionStringSQL());

string query = $"SELECT TOP 1 hc.ID FROM HistorialCartones hc WITH(NOLOCK) " +
$" INNER JOIN AspNetUsers anu WITH(NOLOCK) ON hc.UsuarioId = anu.Id AND UsuarioId = '{userName}' " +
$"WHERE EstadoId = 1 ORDER BY Fecha DESC";

var response = cnn.QueryFirstOrDefault<string>(query);

return response;
}

public List<CartonModel> GetCartonesByPartidaId(string partidaId) {
using var cnn = new SqlConnection(new ConnectionDB().ConnectionStringSQL());

string query = $"SELECT * FROM Cartones WITH(NOLOCK) WHERE JuegoHistorialId = '{partidaId}'";

var response = cnn.Query<CartonModel>(query).ToList();

return response;
}

public int NewGame(PartidaModel partida)
{
string query = $"INSERT INTO HistorialCartones (Fecha, EstadoId, UsuarioId) VALUES (GETDATE(), @EstadoId, @UsuarioId); SELECT SCOPE_IDENTITY()";
Expand Down Expand Up @@ -112,7 +146,7 @@ public List<BolilleroModel> GetBolillasCantadas(string partidaId) {

using var cnn = new SqlConnection(new ConnectionDB().ConnectionStringSQL());

string query = $"SELECT * FROM HistorialBolillero WITh(NOLOCK) WHERE JuegoHistorialId = {partidaId} ORDER BY Alta";
string query = $"SELECT * FROM HistorialBolillero WITh(NOLOCK) WHERE JuegoHistorialId = {partidaId} ORDER BY Alta DESC";

var bolillero = cnn.Query<BolilleroModel>(query).ToList();

Expand Down Expand Up @@ -160,10 +194,10 @@ public List<string> GetWinners(string partidaId) {
return winners;
}

public int UpdateEndGame(string partidaId, string inserts)
public int UpdateEndGame(string partidaId, int estadoId, string inserts)
{

string query = $"UPDATE HistorialCartones SET Fecha = GETDATE(){inserts} WHERE ID = {partidaId}";
string query = $"UPDATE HistorialCartones SET Fecha = GETDATE(), EstadoId = {estadoId}{inserts} WHERE ID = {partidaId}";

using var cnn = new SqlConnection(new ConnectionDB().ConnectionStringSQL());

Expand All @@ -186,39 +220,5 @@ public int UpdateEndGame(string partidaId, string inserts)
}
}

public ResponseModel LoadGame()
{
using (var cnn = new SqlConnection(new ConnectionDB().ConnectionStringSQL()))
{
string query = "SELECT hc.Id FROM HistorialCartones hc WITH(NOLOCK) INNER JOIN AspNetUsers u WITH(NOLOCK) ON hc.UsuarioId = u.ID AND hc.EstadoId = 1";

ResponseModel response = new();

try
{
cnn.Open();
int partidaId = cnn.ExecuteScalar<int>(query);

response.Status = true;
response.Data = partidaId.ToString();


}
catch (Exception)
{
response.Status = false;

throw;

}
finally
{
cnn.Close();
}
return response;
};
}


}
}
1 change: 1 addition & 0 deletions JuegoBingoAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
policy =>
{
policy.WithOrigins("https://localhost:7254");

});
});

Expand Down
158 changes: 39 additions & 119 deletions JuegoBingoAPI/Rule/BingoRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ public class BingoRule
{
private readonly int _numeroCartones = 4;

public ResponseModel NewGame(string usuarioId)
public ResponseModel NewGame(string userName)
{
try
{
BingoData data = new();
string userId = data.GetUserIdByName(userName);

//Creo
PartidaModel miPartida = new()
{
Fecha = DateTime.Now,
EstadoId = 1,
UsuarioId = usuarioId
UsuarioId = userId
};

//Guardo
BingoData data = new();
int partidaId = data.NewGame(miPartida);

var misCartones = new List<CartonModel>();
Expand Down Expand Up @@ -149,7 +151,7 @@ public ResponseModel NewNumber(string partidaId) {
inserts += $", Carton{i + 1} = {checkWinner[i]}";
}

data.UpdateEndGame(partidaId, inserts);
data.UpdateEndGame(partidaId, 3, inserts);

response.Message = $"Ganador: carton {checkWinner}";
response.Data = JsonSerializer.Serialize(checkWinner);
Expand All @@ -164,129 +166,47 @@ public ResponseModel NewNumber(string partidaId) {


}

/* private static ResponseModel NewGamePlayer(List<CartonModel> cartones)
{
var inserts = "";
try
{
//Creo
foreach (var item in cartones)
{
inserts = $"{item.NumeroCarton}, {item.JuegoHistorialId}, '{item.Numeros}'";
// Valido que se ingrese cada uno de los jugadores.
}
//Gueardo
var data = new BingoData();
var jugadores = data.NewGamePlayers(inserts);

return new ResponseModel()
{
Status = true,
Message = "Partida registrada con éxito!",
Data = cartones.ToString()
};
}
catch (Exception)
{
return new ResponseModel()
{
Status = false,
Message = "Ha ocurrido un error al momento de generar la partida! Intente nuevamente mas tarde...",
Data = ""
};
}
}
*/
/*private static ResponseModel NewGamePlayer(int partidaId, PartidaModel partida)
{
var data = new BingoData();
var inserts = "";
var count = 1;
var isOk = true;
/*try
{
foreach (var item in partida.)
{
inserts = $"{count}, {partidaId}, '{item.Numeros}'";
var jugador = data.NewGamePlayers(inserts);
// Valido que se ingrese cada uno de los jugadores.
count++;
if (!jugador.Status)
{
isOk = false;
}
}
if (isOk)
{
return new ResponseModel()
{
Status = true,
Message = "Partida registrada con éxito!",
Data = partida.ToString()
};
}
else
{
return new ResponseModel()
{
Status = false,
Message = "Ha ocurrido un error al momento de generar la partida! Intente nuevamente mas tarde...",
Data = partida.ToString()
};
}
public ResponseModel GetParty(string userName) {

}
catch (Exception ex)
{
return new ResponseModel()
{
Status = false,
Message = ex.Message,
Data = ""
};
}
}
*/
/*public Response NewGame(int usuarioId)
{
try
ResponseModel response = new()
{
var data = new BingoData();
Status = true,
Message = "",
Data = ""
};

var existe = data.ExistePartida(usuarioId);
BingoData data = new();
string userId = data.GetUserIdByName(userName);

if (existe > 0)
{
return new Response()
{
Status = false,
Message = "Ya existe una Partida sin terminar, desea comomenzar una nueva?",
Data = "0"
};
}
else
{
PartidaDto miPartida = new();
miPartida.ArmarPartida(4);
var response = data.NuevaPartidaJSON(miPartida);
return new Response()
{
Status = true,
Message = "Partida registrada con éxito!",
Data = response
};
}
}
catch (Exception ex)
var partidaId = data.GetPartidaByUserName(userId);
var cartones = data.GetCartonesByPartidaId(partidaId);


if (partidaId != "" && cartones.Count > 0)
{
return new Response()
//List<string> numbers = new();
var bolillero = data.GetBolillasCantadas(partidaId);

//foreach (var number in bolillero) {
// numbers.Add(number.Numeros.ToString());
//}

var miPartida = new
{
Status = false,
Message = ex.Message,
Data = "0"
PartidaId = Int32.Parse(partidaId),
Cartones = cartones,
Bolillas = bolillero
};

response.Data = JsonSerializer.Serialize(miPartida);
}
else
{
response.Status = false;
}
}*/

return response;
}
}
}

0 comments on commit a2e0d77

Please sign in to comment.