Changing from js to golang
This commit is contained in:
@@ -2,12 +2,13 @@ package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
_ "github.com/lib/pq"
|
||||
"gitlab.com/kbr4/svevijesti/internal/model"
|
||||
"html/template"
|
||||
"math"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
"gitlab.com/kbr4/svevijesti/internal/model"
|
||||
)
|
||||
|
||||
func InsertArticle(store *Store, article model.ScrapedArticle) (err error) {
|
||||
@@ -186,3 +187,48 @@ func PreviousAndNextArticleUrlByID(store *Store, ID int) (nextUrl string, previo
|
||||
|
||||
return nextResult, previousResult, nil
|
||||
}
|
||||
|
||||
func ArticleByCategory(store *Store, day time.Time) (cateogry []model.DisplayArticle, err error) {
|
||||
|
||||
result := []model.DisplayArticle{}
|
||||
query, err := store.Prepare(`select id,title,content,slug,created_at,source_id,category from articles where created_at > $1 and created_at < $2 and LENGTH(content) > 10 order by id desc;`)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
defer query.Close()
|
||||
|
||||
tomorow := day.AddDate(0, 0, 1)
|
||||
todayDate := day.Format("2024-01-26")
|
||||
tomorrowDate := tomorow.Format("2024:01:26")
|
||||
|
||||
rows, err := query.Query(todayDate, tomorrowDate)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
r := model.DisplayArticle{}
|
||||
err = rows.Scan(&r.ID, &r.Title, &r.Content, &r.Slug, &r.CreatedAt, &r.OriginalUrl, &r.SourceId, &r.CreatedAt, &r.Category)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
ago := time.Now().Sub(r.CreatedAt)
|
||||
hours := ago.Hours()
|
||||
|
||||
if hours < 1 {
|
||||
r.FormatedCreatedAt = fmt.Sprintf("Prije %d sati.", int(math.Floor(ago.Minutes())))
|
||||
} else if hours > 24 {
|
||||
r.FormatedCreatedAt = r.CreatedAt.Format("28.01.2024. 01:03:05")
|
||||
} else {
|
||||
r.FormatedCreatedAt = fmt.Sprintf("Prije %d sati.", int(math.Floor(ago.Minutes())))
|
||||
}
|
||||
|
||||
r.SourceName = model.SourceName(r.SourceId)
|
||||
|
||||
result = append(result, r)
|
||||
}
|
||||
return result, nil
|
||||
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ type DisplayArticle struct {
|
||||
CreatedAt time.Time
|
||||
FormatedCreatedAt string
|
||||
SourceName string
|
||||
Category string
|
||||
}
|
||||
|
||||
const (
|
||||
|
||||
@@ -2,11 +2,12 @@ package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"gitlab.com/kbr4/svevijesti/internal/database"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"gitlab.com/kbr4/svevijesti/internal/database"
|
||||
)
|
||||
|
||||
func rootHandler(wr http.ResponseWriter, req *http.Request) {
|
||||
|
||||
34
internal/server/category.go
Normal file
34
internal/server/category.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"gitlab.com/kbr4/svevijesti/internal/database"
|
||||
"gitlab.com/kbr4/svevijesti/internal/model"
|
||||
)
|
||||
|
||||
func categoryHandler(wr http.ResponseWriter, r *http.Request) {
|
||||
|
||||
store, err := database.Connect()
|
||||
|
||||
if err != nil {
|
||||
http.Error(wr, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
articles, err := database.ArticleByCategory(store, time.Now())
|
||||
|
||||
articlesByCategory := make(map[string][]model.DisplayArticle)
|
||||
|
||||
for _, article := range articles {
|
||||
articlesByCategory[article.Category] = append(articlesByCategory[article.Category], article)
|
||||
}
|
||||
|
||||
for category, articles := range articlesByCategory {
|
||||
data := map[string]interface{}{"Category": category, "Articles": articles}
|
||||
err := templates.ExecuteTemplate(wr, "categoryHTML", data)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,12 @@ package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
var tPath = "./web/tpl/"
|
||||
@@ -42,5 +43,7 @@ func CreateRoutes() *mux.Router {
|
||||
r.HandleFunc("/dan/{date}", dailyArticlesHandler)
|
||||
r.HandleFunc("/{id:[0-9]+}/{slug}", articleHandler)
|
||||
r.HandleFunc("/", rootHandler)
|
||||
r.HandleFunc("/weather", WeatherHandler)
|
||||
r.HandleFunc("/{category}", categoryHandler)
|
||||
return r
|
||||
}
|
||||
|
||||
97
internal/server/weather.go
Normal file
97
internal/server/weather.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const apiKey = "abb35e21bdcbad6d1b00141a2b25cf5a"
|
||||
|
||||
type WeatherData struct {
|
||||
Coord struct {
|
||||
Lat float64 `json:"lat"`
|
||||
Lon float64 `json:"lon"`
|
||||
} `json:"coord"`
|
||||
Weather []struct {
|
||||
Description string `json:"description"`
|
||||
Icon string `json:"icon"`
|
||||
} `json:"weather"`
|
||||
Main struct {
|
||||
Temp float64 `json:"temp"`
|
||||
FellsLike float64 `json:"fells_like"`
|
||||
Preassure int `json:"preassure"`
|
||||
Humidity int `json:"humidity"`
|
||||
TempMin float64 `json:"temp_min"`
|
||||
TempMax float64 `json:"temp_max"`
|
||||
} `json:"main"`
|
||||
Wind struct {
|
||||
Speed float64 `json:"speed"`
|
||||
Deg float64 `json:"deg"`
|
||||
} `json:"wind"`
|
||||
Clouds struct {
|
||||
All int `json:"all"`
|
||||
} `json:"clouds"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func getWeather(city string) (WeatherData, error) {
|
||||
url := fmt.Sprintf("http://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=metric&lang=hr", city, apiKey)
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return WeatherData{}, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return WeatherData{}, err
|
||||
}
|
||||
var weatherData WeatherData
|
||||
err = json.Unmarshal(body, &weatherData)
|
||||
if err != nil {
|
||||
return WeatherData{}, err
|
||||
}
|
||||
return weatherData, nil
|
||||
}
|
||||
|
||||
func WeatherHandler(w http.ResponseWriter, r *http.Request) {
|
||||
cities := []string{"Sarajevo", "Banja Luka", "Zenica", "Tuzla", "Mostar"}
|
||||
|
||||
var weatherInfo []WeatherData
|
||||
for _, city := range cities {
|
||||
data, err := getWeather(city)
|
||||
if err != nil {
|
||||
fmt.Printf("Error fetching weather for %s: %v\n", city, err)
|
||||
continue
|
||||
}
|
||||
weatherInfo = append(weatherInfo, data)
|
||||
}
|
||||
|
||||
title := "Vremenska Prognoza"
|
||||
data := map[string]interface{}{
|
||||
"title": title,
|
||||
"weatherInfo": weatherInfo,
|
||||
}
|
||||
|
||||
err := templates.ExecuteTemplate(w, "fullweatherHTML", data)
|
||||
if err != nil {
|
||||
fmt.Println("Error executing template:", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
for _, info := range weatherInfo {
|
||||
widgetData := map[string]interface{}{
|
||||
"Temperature": info.Main.Temp,
|
||||
"City": info.Name,
|
||||
}
|
||||
err := templates.ExecuteTemplate(w, "weatherwidgetHTML", widgetData)
|
||||
if err != nil {
|
||||
fmt.Println("Error executing template:", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user