summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyren223 <Kyren223@proton.me>2024-12-01 15:47:54 +0200
committerKyren223 <Kyren223@proton.me>2024-12-01 15:47:54 +0200
commit3e9c0ee6a1e765d42d54acb71c10ae7e67353a17 (patch)
tree97316a3002f4ee12d0ac3eadeaf720c7a950c37a
parent78a38f508e7b9d2389c9c422c2d164b9059b03f8 (diff)
Implemented frequency creation on both client and server
-rw-r--r--internal/client/ui/core/frequencycreation/frequencycreation.go5
-rw-r--r--internal/client/ui/core/network/network.go6
-rw-r--r--internal/data/frequencies.sql.go4
-rw-r--r--internal/data/models.go2
-rw-r--r--internal/packet/models.go1
-rw-r--r--internal/server/api/api.go5
-rw-r--r--migrations/20241026174345_init.sql18
7 files changed, 27 insertions, 14 deletions
diff --git a/internal/client/ui/core/frequencycreation/frequencycreation.go b/internal/client/ui/core/frequencycreation/frequencycreation.go
index 43fca8f..bb4d398 100644
--- a/internal/client/ui/core/frequencycreation/frequencycreation.go
+++ b/internal/client/ui/core/frequencycreation/frequencycreation.go
@@ -60,10 +60,10 @@ const (
const (
NameField = iota
- ColorField
ReadWriteField
ReadOnlyField
NoAccessField
+ ColorField
CreateField
FieldCount
)
@@ -108,7 +108,7 @@ func New(network snowflake.ID) Model {
}
return nil
}
- color.SetValue(string(colors.White)[1:])
+ color.SetValue(string(packet.DefaultFrequencyColor)[1:])
return Model{
name: name,
@@ -116,6 +116,7 @@ func New(network snowflake.ID) Model {
lastColor: lipgloss.Color("#" + color.Value()),
perms: packet.PermReadWrite,
create: blurredCreate,
+ network: network,
nameWidth: nameWidth,
precomputedStyle: lipgloss.NewStyle().Width(nameWidth / 3),
diff --git a/internal/client/ui/core/network/network.go b/internal/client/ui/core/network/network.go
index 2d7b44f..69d85c0 100644
--- a/internal/client/ui/core/network/network.go
+++ b/internal/client/ui/core/network/network.go
@@ -61,11 +61,7 @@ func (m Model) View() string {
builder.WriteString("\n")
for i, frequency := range m.Network().Frequencies {
- color := colors.White
- if frequency.HexColor != nil {
- color = lipgloss.Color(*frequency.HexColor)
- }
- frequencyStyle := frequencyStyle.Foreground(color)
+ frequencyStyle := frequencyStyle.Foreground(lipgloss.Color(frequency.HexColor))
if m.index == i {
frequencyStyle = frequencyStyle.Background(colors.BackgroundHighlight)
}
diff --git a/internal/data/frequencies.sql.go b/internal/data/frequencies.sql.go
index 4487ac0..024c069 100644
--- a/internal/data/frequencies.sql.go
+++ b/internal/data/frequencies.sql.go
@@ -27,7 +27,7 @@ type CreateFrequencyParams struct {
ID snowflake.ID
NetworkID snowflake.ID
Name string
- HexColor *string
+ HexColor string
Perms int64
}
@@ -123,7 +123,7 @@ RETURNING id, network_id, name, hex_color, perms, position
`
type SetFrequencyColorParams struct {
- HexColor *string
+ HexColor string
ID snowflake.ID
}
diff --git a/internal/data/models.go b/internal/data/models.go
index f53d726..3b5425f 100644
--- a/internal/data/models.go
+++ b/internal/data/models.go
@@ -13,7 +13,7 @@ type Frequency struct {
ID snowflake.ID
NetworkID snowflake.ID
Name string
- HexColor *string
+ HexColor string
Perms int64
Position int64
}
diff --git a/internal/packet/models.go b/internal/packet/models.go
index 5e4a70b..e0b39d7 100644
--- a/internal/packet/models.go
+++ b/internal/packet/models.go
@@ -3,6 +3,7 @@ package packet
const (
MaxIconBytes = 16
DefaultFrequencyName = "main"
+ DefaultFrequencyColor = "#FFFFFF"
MaxFrequencyName = 32
)
diff --git a/internal/server/api/api.go b/internal/server/api/api.go
index 4370d90..533a9b0 100644
--- a/internal/server/api/api.go
+++ b/internal/server/api/api.go
@@ -130,7 +130,7 @@ func CreateNetwork(ctx context.Context, sess *session.Session, request *packet.C
ID: sess.Manager().Node().Generate(),
NetworkID: network.ID,
Name: packet.DefaultFrequencyName,
- HexColor: nil,
+ HexColor: packet.DefaultFrequencyColor,
Perms: packet.PermReadWrite,
})
if err != nil {
@@ -259,6 +259,7 @@ func CreateFrequency(ctx context.Context, sess *session.Session, request *packet
NetworkID: request.Network,
})
if err == sql.ErrNoRows {
+ log.Println("Err", err)
return &packet.Error{Error: "user entry in network doesn't exist"}
}
if err != nil {
@@ -290,7 +291,7 @@ func CreateFrequency(ctx context.Context, sess *session.Session, request *packet
ID: sess.Manager().Node().Generate(),
NetworkID: request.Network,
Name: request.Name,
- HexColor: &request.HexColor,
+ HexColor: request.HexColor,
Perms: int64(request.Perms),
})
if err != nil {
diff --git a/migrations/20241026174345_init.sql b/migrations/20241026174345_init.sql
index 6fa0b93..e4f0f78 100644
--- a/migrations/20241026174345_init.sql
+++ b/migrations/20241026174345_init.sql
@@ -37,7 +37,7 @@ CREATE TABLE frequencies (
id INTEGER PRIMARY KEY,
network_id INT NOT NULL REFERENCES networks (id) ON DELETE CASCADE,
name TEXT NOT NULL,
- hex_color TEXT,
+ hex_color TEXT NOT NULL,
perms INT NOT NULL CHECK (perms IN (0, 1, 2)),
-- 0 no access | 1 read | 2 read & write
position INT NOT NULL
@@ -45,7 +45,7 @@ CREATE TABLE frequencies (
CREATE TABLE users_networks (
user_id INT NOT NULL REFERENCES users (id),
- network_id INT NOT NULL REFERENCES networks (id) ON DELETE CASCADE,
+ network_id INT NOT NULL REFERENCES networks (id),
joined_at TEXT NOT NULL DEFAULT current_timestamp,
is_member BOOLEAN NOT NULL CHECK (is_member IN (false, true)) DEFAULT true,
is_admin BOOLEAN NOT NULL CHECK (is_admin IN (false, true)) DEFAULT false,
@@ -99,6 +99,20 @@ BEGIN
END
-- +goose StatementEnd
+-- +goose StatementBegin
+CREATE TRIGGER on_network_delete
+AFTER DELETE ON networks
+BEGIN
+ UPDATE users_networks SET
+ position = position - 1
+ WHERE user_id IN (
+ SELECT user_id FROM users_networks WHERE network_id = OLD.network_id
+ ) AND position > OLD.position;
+
+ DELETE FROM users_networks WHERE network_id = OLD.network_id;
+END
+-- +goose StatementEnd
+
-- +goose Down
DROP TABLE messages;
DROP TABLE users;