Skip to content

Commit

Permalink
updates to search for users
Browse files Browse the repository at this point in the history
  • Loading branch information
ruskaof committed Mar 20, 2024
1 parent 043fc4c commit cbcc40b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
10 changes: 10 additions & 0 deletions src/dto/dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dto

type UserInfo struct {
UserId int64 `json:"id"`
}

type UserResponse struct {
UserId int64 `json:"userId"`
Username string `json:"username"`
}
9 changes: 5 additions & 4 deletions src/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handler
import (
"dwelt/src/auth"
"dwelt/src/config"
"dwelt/src/dto"
"dwelt/src/service/usrserv"
"dwelt/src/utils"
"dwelt/src/ws/chat"
Expand Down Expand Up @@ -55,7 +56,7 @@ func handlerLogin(w http.ResponseWriter, r *http.Request) {

token := auth.GenerateToken(userId)
w.Header().Set("Authorization", "Bearer "+token)
utils.WriteJson(w, userInfo{UserId: userId})
utils.WriteJson(w, dto.UserInfo{UserId: userId})
}

func handlerRegister(w http.ResponseWriter, r *http.Request) {
Expand All @@ -78,12 +79,12 @@ func handlerRegister(w http.ResponseWriter, r *http.Request) {

token := auth.GenerateToken(userId)
w.Header().Set("Authorization", "Bearer "+token)
utils.WriteJson(w, userInfo{UserId: userId})
utils.WriteJson(w, dto.UserInfo{UserId: userId})
}

func handlerSearchUsers(w http.ResponseWriter, r *http.Request) {
substring := r.URL.Query().Get("substring")
users, err := usrserv.SearchUsers(substring, DEFAULT_LIMIT)
prefix := r.URL.Query().Get("prefix")
users, err := usrserv.SearchUsers(prefix, DEFAULT_LIMIT)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
Expand Down
5 changes: 0 additions & 5 deletions src/handler/model.go

This file was deleted.

17 changes: 0 additions & 17 deletions src/service/usrserv/dto.go

This file was deleted.

13 changes: 13 additions & 0 deletions src/service/usrserv/mapping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package usrserv

import (
"dwelt/src/dto"
"dwelt/src/model/entity"
)

func userResponseFromEntity(user entity.User) dto.UserResponse {
return dto.UserResponse{
UserId: user.ID,
Username: user.Username,
}
}
7 changes: 4 additions & 3 deletions src/service/usrserv/usrserv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package usrserv

import (
"crypto/sha512"
"dwelt/src/dto"
"dwelt/src/model/dao"
"dwelt/src/model/entity"
"encoding/hex"
Expand Down Expand Up @@ -58,14 +59,14 @@ func RegisterUser(username string, password string) (userId int64, duplicate boo
return
}

func SearchUsers(substring string, limit int) (users []UserResponse, err error) {
func SearchUsers(prefix string, limit int) (users []dto.UserResponse, err error) {
var usersEntity []entity.User
err = dao.Db.Where("username LIKE ?", substring+"%").Limit(limit).Find(&usersEntity).Error
err = dao.Db.Where("username LIKE ?", prefix+"%").Limit(limit).Find(&usersEntity).Error
if err != nil {
slog.Error(err.Error(), "method", "SearchUsers")
}

users = make([]UserResponse, len(usersEntity))
users = make([]dto.UserResponse, len(usersEntity))
for i, user := range usersEntity {
users[i] = userResponseFromEntity(user)
}
Expand Down

0 comments on commit cbcc40b

Please sign in to comment.