Skip to content

Instantly share code, notes, and snippets.

@gavilanch
Last active February 10, 2023 12:43
Show Gist options
  • Save gavilanch/d9f4a5dc803e4b4ff63ea3a296e5320e to your computer and use it in GitHub Desktop.
Save gavilanch/d9f4a5dc803e4b4ff63ea3a296e5320e to your computer and use it in GitHub Desktop.
[HttpPost]
public async Task<ActionResult<int>> Post(Pelicula pelicula)
{
if (!string.IsNullOrWhiteSpace(pelicula.Poster))
{
var poster = Convert.FromBase64String(pelicula.Poster);
pelicula.Poster = await almacenadorArchivos.GuardarArchivo(poster,
".jpg", contenedor);
}
EscribirOrdenActores(pelicula);
await AgregarPeliculaConSP(pelicula);
//context.Add(pelicula);
//await context.SaveChangesAsync();
return pelicula.Id;
}
private async Task AgregarPeliculaConSP(Pelicula pelicula)
{
// conexion con Dapper
using var conn = new SqlConnection(connectionString);
// insertar pelicula SP
var id = await conn.QuerySingleAsync<int>($"InsertarPelicula", new
{
titulo = pelicula.Titulo,
resumen = pelicula.Resumen,
enCartelera = pelicula.EnCartelera,
trailer = pelicula.Trailer,
lanzamiento = pelicula.Lanzamiento?.ToString("yyyy-MM-dd"),
poster = pelicula.Poster,
},
commandType: System.Data.CommandType.StoredProcedure);
// insertar generos SP
if (pelicula.GenerosPelicula is not null && pelicula.GenerosPelicula.Count > 0)
{
var generosPeliculas = pelicula.GenerosPelicula.Select(x => new { PeliculaId = id, x.GeneroId });
await conn.ExecuteAsync($"InsertarGeneroPelicula", generosPeliculas,
commandType: System.Data.CommandType.StoredProcedure);
}
// insertar actores SP
// asignar pelicula Id
pelicula.Id = id;
}
ALTER PROCEDURE InsertarGeneroPelicula
@peliculaId int,
@generoId int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
INSERT INTO GenerosPeliculas (PeliculaId, GeneroId)
Values (@peliculaId, @generoId);
END
GO
ALTER PROCEDURE InsertarPelicula
@Titulo nvarchar(max),
@Resumen nvarchar(max) = NULL,
@EnCartelera bit,
@Trailer nvarchar(max) = NULL,
@Lanzamiento datetime2 = NULL,
@Poster nvarchar(max) = NULL
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
INSERT INTO Peliculas (Titulo, Resumen, EnCartelera, Trailer, Lanzamiento, Poster)
Values(@Titulo, @Resumen, @EnCartelera, @Trailer, @Lanzamiento, @Poster)
SELECT SCOPE_IDENTITY();
END
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment