Skip to content

Commit 4786087

Browse files
committed
Reorganize circles_repo and user_repo
1 parent 04ba632 commit 4786087

4 files changed

Lines changed: 49 additions & 48 deletions

File tree

backend/circles/handler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ func SearchTextHandler(w http.ResponseWriter, r *http.Request) {
270270
json.NewEncoder(w).Encode("HTTP 400 bad request")
271271
return
272272
}
273+
fmt.Println("Circle ID: " + searchData.CircleID + " Content: " + searchData.Content)
273274
results, err := postgres.SearchCircle(searchData.CircleID, searchData.Content)
274275
if err != nil {
275276
fmt.Printf("Failed to search circles\n")

backend/postgres/circles_repo.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,49 @@ func GetInviteUsersInCircle(userID string, circleID string) ([]models.User, erro
223223
return users, nil
224224
}
225225

226+
func AddUsersToCircle(circleID string, userIDs []string) error {
227+
ctx := context.Background()
228+
conn, err := pool.Acquire(ctx)
229+
if err != nil {
230+
fmt.Fprintf(os.Stderr, "Unable to acquire a connection from the pool: %v\n", err)
231+
return err
232+
}
233+
defer conn.Release()
234+
235+
tx, err := conn.Begin(ctx)
236+
if err != nil {
237+
fmt.Fprintf(os.Stderr, "Unable to begin transaction: %v\n", err)
238+
}
239+
defer func() {
240+
if err != nil {
241+
_ = tx.Rollback(ctx)
242+
}
243+
}()
244+
245+
for _, id := range userIDs {
246+
_, err = tx.Exec(
247+
ctx,
248+
`
249+
INSERT INTO users_circles (user_id, circle_id, joined_at, role)
250+
VALUES ($1, $2, NOW(), 'member');
251+
`,
252+
id,
253+
circleID)
254+
if err != nil {
255+
fmt.Fprintf(os.Stderr, "Error inserting into users_circles: %v\n", err)
256+
return err
257+
}
258+
}
259+
260+
err = tx.Commit(ctx)
261+
if err != nil {
262+
fmt.Fprintf(os.Stderr, "Unable to commit transaction: %v\n", err)
263+
return err
264+
}
265+
266+
return nil
267+
}
268+
226269
func EditRoleInCircle(circleID string, targetUserID string, role string) error {
227270
ctx := context.Background()
228271
conn, err := pool.Acquire(ctx)

backend/postgres/migrations/000005_init.up.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ RETURNS TABLE (
2222
BEGIN
2323
IF NOT EXISTS (
2424
SELECT *
25-
FROM users_circles
26-
WHERE user_id = request_user_id
27-
AND circle_id = circle_id_input
28-
AND role = 'admin'
25+
FROM users_circles uc
26+
WHERE uc.user_id = request_user_id
27+
AND uc.circle_id = circle_id_input
28+
AND uc.role = 'admin'
2929
) THEN
3030
RAISE EXCEPTION 'permission denied';
3131
END IF;
@@ -34,7 +34,7 @@ BEGIN
3434
SELECT u.id, u.username, uc.role
3535
FROM users u
3636
INNER JOIN users_circles uc ON u.id = uc.user_id
37-
WHERE u.id != p_requester_id AND uc.circle_id = p_circle_id
37+
WHERE u.id != request_user_id AND uc.circle_id = circle_id_input
3838
ORDER BY u.username ASC;
3939
END;
4040
$$ LANGUAGE plpgsql;

backend/postgres/user_repo.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -83,49 +83,6 @@ func InsertRefreshToken(userID string, token uuid.UUID) {
8383
}
8484
}
8585

86-
func AddUsersToCircle(circleID string, userIDs []string) error {
87-
ctx := context.Background()
88-
conn, err := pool.Acquire(ctx)
89-
if err != nil {
90-
fmt.Fprintf(os.Stderr, "Unable to acquire a connection from the pool: %v\n", err)
91-
return err
92-
}
93-
defer conn.Release()
94-
95-
tx, err := conn.Begin(ctx)
96-
if err != nil {
97-
fmt.Fprintf(os.Stderr, "Unable to begin transaction: %v\n", err)
98-
}
99-
defer func() {
100-
if err != nil {
101-
_ = tx.Rollback(ctx)
102-
}
103-
}()
104-
105-
for _, id := range userIDs {
106-
_, err = tx.Exec(
107-
ctx,
108-
`
109-
INSERT INTO users_circles (user_id, circle_id, joined_at, role)
110-
VALUES ($1, $2, NOW(), 'member');
111-
`,
112-
id,
113-
circleID)
114-
if err != nil {
115-
fmt.Fprintf(os.Stderr, "Error inserting into users_circles: %v\n", err)
116-
return err
117-
}
118-
}
119-
120-
err = tx.Commit(ctx)
121-
if err != nil {
122-
fmt.Fprintf(os.Stderr, "Unable to commit transaction: %v\n", err)
123-
return err
124-
}
125-
126-
return nil
127-
}
128-
12986
func GetUsername(userID string) (string, error) {
13087
ctx := context.Background()
13188
conn, err := pool.Acquire(ctx)

0 commit comments

Comments
 (0)