Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yurii dev #30

Merged
merged 3 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ getGitUdates.sh
# corrected fileName
getGitUpdates.sh

mainDB.db
mainDB.db
mainDB.db
119 changes: 21 additions & 98 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"log"
"net/http"

"github.com/Pomog/ForumFFF/db_driver"
"github.com/Pomog/ForumFFF/internal/config"
"github.com/Pomog/ForumFFF/internal/handler"
"github.com/Pomog/ForumFFF/internal/models"
"github.com/Pomog/ForumFFF/internal/renderer"
"github.com/Pomog/ForumFFF/internal/repository/dbrepo"
"github.com/Pomog/ForumFFF/internal/repository"
"github.com/google/uuid"
)

Expand All @@ -22,47 +21,13 @@ var sessionID uuid.UUID

func main() {

// gob.Register() function is used to inform the encoding/gob package about custom types that may be encoded or decoded using the gob encoding format.
gob.Register(models.User{})
gob.Register(models.Thread{})
gob.Register(models.Votes{})
gob.Register(models.Post{})

sessionID = uuid.New()

app.UserLogin = &sessionID

newUser := models.User{
UserName: "test1",
Password: "123",
FirstName: "testFirstName1",
LastName: "testLastName1",
Email: "test1@mail.com",
}

db, _ := db_driver.GetDB()

repo := dbrepo.NewSQLiteRepo(db, &app)

repo.CreatetUser(newUser)

userNameToFind := "test1"
userNameToFind2 := "noSuchUser"
userEmailToFind := "test1@mail.com"

isPresent, _ := repo.UserPresent(userNameToFind, userEmailToFind)
log.Printf("User %s present - %v", userNameToFind, isPresent)
fmt.Println("--------------------------------------------------")
isPresent2, _ := repo.UserPresent(userNameToFind2, userEmailToFind)
log.Printf("User %s present - %v", userNameToFind2, isPresent2)

// -------------------------------------------------------------

err := run()
db, err := run()
if err != nil {
log.Fatal(err)
}

defer db.SQL.Close()

fmt.Printf("Server starting on port %s\n", Port)

srv := &http.Server{
Expand All @@ -75,83 +40,41 @@ func main() {

}

func createDB() {
db_driver.MakeDBTables()

newUser := models.User{
UserName: "test1",
Password: "123",
FirstName: "testFirstName1",
LastName: "testLastName1",
Email: "test1@mail.com",
}

db, _ := db_driver.GetDB()

repo := dbrepo.NewSQLiteRepo(db, &app)

repo.CreatetUser(newUser)

userNameToFind := "test1"
userNameToFind2 := "noSuchUser"
userEmailToFind := "test1@mail.com"
func run() (*repository.DataBase, error) {
fmt.Println("Starting application")

isPresent, _ := repo.UserPresent(userNameToFind, userEmailToFind)
log.Printf("User %s present - %v", userNameToFind, isPresent)
fmt.Println("--------------------------------------------------")
isPresent2, _ := repo.UserPresent(userNameToFind2, userEmailToFind)
log.Printf("User %s present - %v", userNameToFind2, isPresent2)
sessionID = uuid.New()

}
app.UserLogin = &sessionID

func run() error {
fmt.Println("Starting application")
// gob.Register() function is used to inform the encoding/gob package about custom types that may be encoded or decoded using the gob encoding format.
gob.Register(models.User{})
gob.Register(models.Thread{})
gob.Register(models.Votes{})
gob.Register(models.Post{})

// change this to true when in production
app.InProduction = false

//cookies should be set

repository.MakeDBTables()

db, _ := repository.GetDB()

repo := handler.NewRepo(&app, db)

tc, err := renderer.CreateTemplateCache()
if err != nil {
log.Fatal("cannot create template cache")
return err
return nil, err
}
app.TemplateCache = tc
app.UseCache = false

repo := handler.NewRepo(&app)
handler.NewHandlers(repo)

renderer.NewTemplate(&app)

return nil
return db, nil
}

// db_driver.MakeDBTables()

// newUser := models.User{
// UserName: "test1",
// Password: "123",
// FirstName: "testFirstName1",
// LastName: "testLastName1",
// Email: "test1@mail.com",
// }

// db, _ := db_driver.GetDB()

// repo := dbrepo.NewSQLiteRepo(db, &app)

// repo.CreatetUser(newUser)

// userNameToFind := "test1"
// userNameToFind2 := "noSuchUser"
// userEmailToFind := "test1@mail.com"

// isPresent, _ := repo.UserPresent(userNameToFind, userEmailToFind)
// log.Printf("User %s present - %v", userNameToFind, isPresent)
// fmt.Println("--------------------------------------------------")
// isPresent2, _ := repo.UserPresent(userNameToFind2, userEmailToFind)
// log.Printf("User %s present - %v", userNameToFind2, isPresent2)

// -------------------------------------------------------------
3 changes: 3 additions & 0 deletions cmd/web/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func routes(a *config.AppConfig) http.Handler {
mux.Handle(fmt.Sprintf("/static/%s/", static), http.StripPrefix(fmt.Sprintf("/static/%s/", static), http.FileServer(http.Dir(fmt.Sprintf("static/%s", static)))))
}

// fileServer := http.FileServer(http.Dir("./static/"))
// mux.Handle("/static/*", http.StripPrefix("/static", fileServer))

mux.HandleFunc("/", handler.Repo.LoginHandler)
mux.HandleFunc("/home", handler.Repo.HomeHandler)
mux.HandleFunc("/theme", handler.Repo.ThemeHandler)
Expand Down
7 changes: 0 additions & 7 deletions db_driver/struct_db.go

This file was deleted.

17 changes: 12 additions & 5 deletions internal/handler/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/Pomog/ForumFFF/internal/forms"
"github.com/Pomog/ForumFFF/internal/models"
"github.com/Pomog/ForumFFF/internal/renderer"
"github.com/Pomog/ForumFFF/internal/repository"
"github.com/Pomog/ForumFFF/internal/repository/dbrepo"
)

// Repo the repository used by the handlers
Expand All @@ -16,12 +18,14 @@ var Repo *Repository
// Repositroy is the repository type
type Repository struct {
App *config.AppConfig
DB repository.DatabaseInt
}

// NewRepo creates a new repository
func NewRepo(a *config.AppConfig) *Repository {
func NewRepo(a *config.AppConfig, db *repository.DataBase) *Repository {
return &Repository{
App: a,
DB: dbrepo.NewSQLiteRepo(a, db.SQL),
}
}

Expand Down Expand Up @@ -66,12 +70,15 @@ func (m *Repository) LoginHandler(w http.ResponseWriter, r *http.Request) {
})
return
}
// if there is no error, we upload Form data into our Session
//WHAT to use here?

//Redirecting when receiving POST request
http.Redirect(w, r, "/home", http.StatusSeeOther)
// Check if User is Presaent in the DB, ERR should be handled
result, _ := m.DB.UserPresent(loginData.UserName, loginData.Email)
if result {
http.Redirect(w, r, "/home", http.StatusSeeOther)
}

// if there is no error, we upload Form data into our Session
//WHAT to use here?
}

// MainHandler is a method of the Repository struct that handles requests to the main page.
Expand Down
21 changes: 13 additions & 8 deletions db_driver/db_funcs.go → internal/repository/db_funcs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package db_driver
package repository

import (
"database/sql"
Expand All @@ -8,17 +8,19 @@ import (
_ "github.com/mattn/go-sqlite3"
)

var dbConnSQL = &DataBase{}

func MakeDBTables() (*sql.DB, error) {
database, err := GetDB()
dbConn, err := GetDB()
if err != nil {
log.Println(err)
return database, err
return dbConn.SQL, err
}

sqlQuerys := getQuerys()

for _, sqlQury := range sqlQuerys {
statement, errPrepare := database.Prepare(sqlQury)
statement, errPrepare := dbConn.SQL.Prepare(sqlQury)
if errPrepare != nil {
fmt.Println("errPrepare:", errPrepare)
}
Expand All @@ -27,14 +29,14 @@ func MakeDBTables() (*sql.DB, error) {

_, err = statement.Exec()
if err != nil {
return database, err
return dbConn.SQL, err
}
}

return database, nil
return dbConn.SQL, nil
}

func GetDB() (*sql.DB, error) {
func GetDB() (*DataBase, error) {
database, err := sql.Open("sqlite3", "./mainDB.db")
if err != nil {
return nil, err
Expand All @@ -43,7 +45,10 @@ func GetDB() (*sql.DB, error) {
if errDB != nil {
return nil, errDB
}
return database, nil

dbConnSQL.SQL = database

return dbConnSQL, nil
}

func testDB(db *sql.DB) error {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package db_driver
package repository

import (
"os"
Expand Down Expand Up @@ -71,32 +71,4 @@ func containsString(slice []string, element string) bool {
}
}
return false
}

// db_driver.MakeDBTables()

// newUser := models.User{
// UserName: "test1",
// Password: "123",
// FirstName: "testFirstName1",
// LastName: "testLastName1",
// Email: "test1@mail.com",
// }

// db, _ := db_driver.GetDB()

// repo := dbrepo.NewSQLiteRepo(db, &app)

// repo.CreatetUser(newUser)

// userNameToFind := "test1"
// userNameToFind2 := "noSuchUser"
// userEmailToFind := "test1@mail.com"

// isPresent, _ := repo.UserPresent(userNameToFind, userEmailToFind)
// log.Printf("User %s present - %v", userNameToFind, isPresent)
// fmt.Println("--------------------------------------------------")
// isPresent2, _ := repo.UserPresent(userNameToFind2, userEmailToFind)
// log.Printf("User %s present - %v", userNameToFind2, isPresent2)

// -------------------------------------------------------------
}
18 changes: 14 additions & 4 deletions internal/repository/dbrepo/dbrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@ import (
"database/sql"

"github.com/Pomog/ForumFFF/internal/config"
"github.com/Pomog/ForumFFF/internal/repository"
)

type sqliteBDRepo struct {
type SqliteBDRepo struct {
App *config.AppConfig
DB *sql.DB
}

func NewSQLiteRepo (conn *sql.DB, a *config.AppConfig) *sqliteBDRepo {
return &sqliteBDRepo{
// Repo is the repository used by the handlers
var Repo *Repository

// Repository is the repository type
type Repository struct {
App *config.AppConfig
DB repository.DatabaseInt
}

func NewSQLiteRepo(a *config.AppConfig, conn *sql.DB) *SqliteBDRepo {
return &SqliteBDRepo{
App: a,
DB: conn,
DB: conn,
}
}
6 changes: 3 additions & 3 deletions internal/repository/dbrepo/sqllite.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/Pomog/ForumFFF/internal/models"
)

func (m *sqliteBDRepo) UserPresent(userName, email string) (bool, error) {
func (m *SqliteBDRepo) UserPresent(userName, email string) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

Expand All @@ -31,7 +31,7 @@ func (m *sqliteBDRepo) UserPresent(userName, email string) (bool, error) {
return true, nil
}

func (m *sqliteBDRepo) CreatetUser(r models.User) error {
func (m *SqliteBDRepo) CreateUser(r models.User) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

Expand All @@ -53,7 +53,7 @@ func (m *sqliteBDRepo) CreatetUser(r models.User) error {
return nil
}

func (m *sqliteBDRepo) CreatetThread(userID int, thread models.Thread) error {
func (m *SqliteBDRepo) CreateThread(userID int, thread models.Thread) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

Expand Down
Loading