Exemplo n.º 1
0
        /// <summary>
        /// Find a rating based on its id in database
        /// </summary>
        /// <param name="ratingId">int</param>
        /// <returns>Rating</returns>
        public Rating FindById(int ratingId)
        {
            Rating rating = null;

            using (_connection = new SqlConnection(_connectionString))
            {
                _connection.Open();
                using (SqlCommand cmd = _connection.CreateCommand())
                {
                    cmd.CommandText = "SELECT * FROM Rating WHERE RatingId = @RatingId";
                    cmd.Parameters.AddWithValue("RatingId", ratingId);
                    SqlDataReader reader = cmd.ExecuteReader();

                    while (reader.Read())
                    {
                        int userId = reader.GetInt32(reader.GetOrdinal("UserId"));

                        rating = new Rating
                        {
                            RatingId = ratingId,
                            User     = _dbUser.FindById(userId),
                            Rate     = reader.GetDouble(reader.GetOrdinal("Rate")),
                            Comment  = reader.GetString(reader.GetOrdinal("Comment"))
                        };
                    }
                }
                _connection.Close();
            }
            return(rating);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Find a location based on id in the database
        /// </summary>
        /// <param name="locationId">int</param>
        /// <returns>Location</returns>
        public Location FindById(int locationId)
        {
            Location location = null;

            using (_connection = new SqlConnection(_connectionString))
            {
                _connection.Open();
                using (SqlCommand cmd = _connection.CreateCommand())
                {
                    cmd.CommandText = "SELECT * FROM Locations WHERE LocationId = @LocationId";
                    cmd.Parameters.AddWithValue("LocationId", locationId);
                    SqlDataReader reader = cmd.ExecuteReader();

                    while (reader.Read())
                    {
                        location = new Location
                        {
                            LocationId          = locationId,
                            Hits                = reader.GetInt32(reader.GetOrdinal("Hits")),
                            IsTopLocation       = reader.GetBoolean(reader.GetOrdinal("IsTopLocation")),
                            LocationName        = reader.GetString(reader.GetOrdinal("LocationName")),
                            LocationDescription = reader.GetString(reader.GetOrdinal("LocationDescription")),
                            Latitude            = reader.GetDouble(reader.GetOrdinal("Latitude")),
                            Longitude           = reader.GetDouble(reader.GetOrdinal("Longitude")),
                            Pictures            = _dbPicture.FindByLocationId(locationId),
                            Ratings             = _dbRating.FindByLocationId(locationId),
                            User                = _dbUser.FindById(reader.GetInt32(reader.GetOrdinal("UserId"))),
                            RowVersion          = (byte[])reader.GetValue(reader.GetOrdinal("rowVersion"))
                        };
                    }
                }
                _connection.Close();
            }
            return(location);
        }