public static int insertarPost(int id, clsPost post) { clsMyConnection clsMyConnection = new clsMyConnection(); SqlConnection sqlConnection = new SqlConnection(); SqlCommand sqlCommand = new SqlCommand(); DateTime localDate = DateTime.Now; sqlCommand.Parameters.AddWithValue("@IDEvento", id); sqlCommand.Parameters.AddWithValue("@NickUsuario", post.NickUsuario); sqlCommand.Parameters.AddWithValue("@Contenido", post.Contenido); sqlCommand.Parameters.AddWithValue("@FechaPost", localDate); //REALMENTE OBTENGO EL VALOR DE LA FECHA CUANDO LLEGA A ESTE PUNTO DEL CÓDIGO, PORQUE SI LO COJO DESDE LA APP HAY MUCHOS //PROBLEMAS DEBIDO AL CAMBIO DE REGIONES ENTRE DISTINTOS DISPOSITIVOS, EL PARSEO Y DEMÁS sqlCommand.CommandText = "INSERT INTO Post (IDEvento, NickUsuario, Contenido, FechaPost) Values (@IDEvento, @NickUsuario, @Contenido, @FechaPost)"; try { sqlConnection = clsMyConnection.getConnection(); sqlCommand.Connection = sqlConnection; int filasAfectadas = sqlCommand.ExecuteNonQuery(); } catch (SqlException) { throw; } finally { clsMyConnection.closeConnection(ref sqlConnection); } return(id); }
// POST: api/Post public int Post(int id, [FromBody] clsPost post) { int filasAfectadas = 0; try { filasAfectadas = clsGestoraPostBL.insertarPost(id, post); } catch (Exception e) { throw new HttpResponseException(HttpStatusCode.ServiceUnavailable); } return(filasAfectadas); }
/// <summary> /// Este método obtiene de la base de datos todos los datos de un evento /// </summary> /// <param name="id">El id del evento</param> /// <returns>Los datos del evento</returns> public static clsEventoDatos obtenerDatosEvento(int id) { clsMyConnection conexion = new clsMyConnection(); SqlConnection sqlConnection = new SqlConnection(); SqlCommand command = new SqlCommand(); SqlCommand command2 = new SqlCommand(); //ESTE ES PARA OBTENER LOS POSTS QUE EXISTEN EN EL EVENTO clsEventoDatos eventoDatos = new clsEventoDatos(); clsEvento evento; clsPost post; List <clsPost> listadoPosts = new List <clsPost>(); SqlDataReader reader; command.Parameters.AddWithValue("@ID", id); command2.Parameters.AddWithValue("@ID", id); command.CommandText = "SELECT ID, Cartel, Fecha, Informacion, Lugar, Genero, Titulo, Nombre, S.Localidad, S.Direccion FROM Evento AS E INNER JOIN Usuario AS U ON E.Lugar = U.Nick INNER JOIN Sala AS S ON U.Nick = S.Nick WHERE ID = @ID"; command2.CommandText = "SELECT NickUsuario, Contenido, FechaPost, Imagen FROM Post AS P INNER JOIN Evento AS E ON P.IDEvento = E.ID INNER JOIN Usuario AS U ON P.NickUsuario = U.Nick WHERE ID = @ID ORDER BY FechaPost DESC"; try { sqlConnection = conexion.getConnection(); command.Connection = sqlConnection; reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { eventoDatos = new clsEventoDatos(); evento = new clsEvento(); evento.Id = (int)reader["ID"]; evento.Cartel = (String)reader["Cartel"]; evento.Fecha = (DateTime)reader["Fecha"]; evento.Informacion = (String)reader["Informacion"]; evento.Lugar = (String)reader["Lugar"]; eventoDatos.Evento = evento; eventoDatos.Evento.Genero = (String)reader["Genero"]; eventoDatos.Evento.Titulo = (String)reader["Titulo"]; eventoDatos.NombreSala = (String)reader["Nombre"]; eventoDatos.Localidad = (String)reader["Localidad"]; eventoDatos.Direccion = (String)reader["Direccion"]; } } reader.Close(); command2.Connection = sqlConnection; reader = command2.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { post = new clsPost(); post.NickUsuario = (String)reader["NickUsuario"]; post.Contenido = (String)reader["Contenido"]; post.Fecha = (DateTime)reader["FechaPost"]; post.Imagen = (String)reader["Imagen"]; listadoPosts.Add(post); } eventoDatos.Posts = listadoPosts; } reader.Close(); } catch (SqlException e) { throw e; } finally { conexion.closeConnection(ref sqlConnection); } return(eventoDatos); }
public static int insertarPost(int id, clsPost post) { return(clsGestoraPostDAL.insertarPost(id, post)); }