~evanj/cms

ref: 4aa45cc85249c542c4d80aed47e25c86145bcc3c cms/internal/s/db/space.go -rw-r--r-- 3.1 KiB
4aa45cc8Evan M Jones Feat(mysql): Moving to mysql. Getting ready for alpha release (just for 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package db

import (
	"encoding/json"
	"fmt"

	"git.sr.ht/~evanj/cms/internal/m/space"
	"git.sr.ht/~evanj/cms/internal/m/user"
)

type Space struct {
	id   string
	name string
	desc string
}

var (
	queryCreateNewSpace       = `INSERT INTO cms_space (NAME, DESCRIPTION) VALUES (?, ?);`
	queryFindSpaceByID        = `SELECT ID, NAME, DESCRIPTION FROM cms_space WHERE ID = ?;`
	queryDeleteSpaceByID      = `DELETE FROM cms_space WHERE ID = ?;`
	queryCreateNewUserToSpace = `INSERT INTO cms_user_to_space (USER_ID, SPACE_ID) VALUES (?, ?);`
	queryFindUserToSpace      = `SELECT SPACE_ID FROM cms_user_to_space WHERE USER_ID = ? AND SPACE_ID = ?;`
	queryFindSpacesByUser     = `SELECT DISTINCT cms_space.ID, NAME, DESCRIPTION FROM cms_space JOIN cms_user_to_space ON cms_space.ID = cms_user_to_space.SPACE_ID WHERE USER_ID = ? LIMIT ? OFFSET ?;`
)

func (db *DB) SpaceNew(user user.User, name, desc string) (space.Space, error) {
	res, err := db.Exec(queryCreateNewSpace, name, desc)
	if err != nil {
		db.log.Println("db.Exec", err)
		return nil, fmt.Errorf("space '%s' already exists", name)
	}

	id, err := res.LastInsertId()
	if err != nil {
		db.log.Println("res.LastInsertId", err)
		return nil, fmt.Errorf("failed to create space")
	}

	var space Space
	if err := db.QueryRow(queryFindSpaceByID, id).Scan(&space.id, &space.name, &space.desc); err != nil {
		db.log.Println("db.QueryRow", err)
		return nil, fmt.Errorf("failed to find space created")
	}

	if _, err := db.Exec(queryCreateNewUserToSpace, user.ID(), space.ID()); err != nil {
		db.log.Println("big problem, failed to connect user to space", err)
		if _, err := db.Exec(queryDeleteSpaceByID, space.ID()); err != nil {
			db.log.Println("even bigger problem, failed to delete orphan space", err)
		}
		return nil, fmt.Errorf("failed to attach space to user")
	}

	return &space, nil
}

func (db *DB) SpaceGet(user user.User, spaceID string) (space.Space, error) {
	var id string

	if err := db.QueryRow(queryFindUserToSpace, user.ID(), spaceID).Scan(&id); err != nil {
		db.log.Println("db.QueryRow", err)
		return nil, fmt.Errorf("failed to find space for user")
	}

	var space Space
	err := db.QueryRow(queryFindSpaceByID, id).Scan(&space.id, &space.name, &space.desc)
	if err != nil {
		db.log.Println("db.Exec", err)
		return nil, fmt.Errorf("failed to find space")
	}

	return &space, nil
}

func (db *DB) SpacesPerUser(user user.User, page int) ([]space.Space, error) {
	var ret []space.Space
	rows, err := db.Query(queryFindSpacesByUser, user.ID(), perPage, perPage*page)
	if err != nil {
		db.log.Println(err)
		return ret, err
	}

	for rows.Next() {
		var space Space
		if err := rows.Scan(&space.id, &space.name, &space.desc); err != nil {
			return nil, err
		}
		ret = append(ret, &space)
	}

	return ret, nil
}

func (s *Space) ID() string {
	return s.id
}

func (s *Space) Name() string {
	return s.name
}

func (s *Space) Desc() string {
	return s.desc
}

func (c *Space) MarshalJSON() ([]byte, error) {
	values := make(map[string]string)
	values["id"] = c.ID()
	values["name"] = c.Name()
	values["desc"] = c.Desc()
	return json.Marshal(values)
}