-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.go
38 lines (34 loc) · 862 Bytes
/
user.go
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
package routes
import (
"database/sql"
"encoding/json"
"errors"
"net/http"
"github.com/go-chi/chi"
"github.com/samber/do"
"github.com/therealpaulgg/ssh-sync-server/pkg/database/repository"
"github.com/therealpaulgg/ssh-sync/pkg/dto"
)
func getUser(i *do.Injector) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userRepo := do.MustInvoke[repository.UserRepository](i)
user, err := userRepo.GetUserByUsername(chi.URLParam(r, "username"))
if errors.Is(err, sql.ErrNoRows) {
w.WriteHeader(http.StatusNotFound)
return
}
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
userDto := dto.UserDto{
Username: user.Username,
}
json.NewEncoder(w).Encode(userDto)
}
}
func UserRoutes(i *do.Injector) chi.Router {
r := chi.NewRouter()
r.Get("/{username}", getUser(i))
return r
}