Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:search functionality #17

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions api/search.go
Original file line number Diff line number Diff line change
@@ -1 +1,84 @@
package api

import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)

type searchByCommunityNameRequest struct {
CommunityName string `json:"communities_name"`
}

func (server *Server) searchByCommunityName(ctx *gin.Context) {
fmt.Println("Line no 14")
var req searchByCommunityNameRequest
fmt.Println("Line no 15")
fmt.Println(req.CommunityName)
err := ctx.ShouldBindJSON(&req)
if err != nil {
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
}
fmt.Println("Line no 21")
fmt.Println("CommunityName: %s", req.CommunityName)

communityName, err := server.store.SearchByCommunityName(ctx, req.CommunityName)
if err != nil {
ctx.JSON(http.StatusNotFound, errorResponse(err))
return
}

ctx.JSON(http.StatusOK, communityName)
return
}

type searchByFullNameRequest struct {
FullName string `json:"full_name"`
}

func (server *Server) searchByFullName(ctx *gin.Context) {
var req searchByFullNameRequest
err := ctx.ShouldBindJSON(&req)
if err != nil {
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
}

profile, err := server.store.SearchByFullName(ctx, req.FullName)
if err != nil {
ctx.JSON(http.StatusNotFound, errorResponse(err))
return
}

ctx.JSON(http.StatusOK, profile)
return
}

type searchCommunityByCommunityTypeRequest struct {
CommunityType string `json:"community_type"`
}

func (server *Server) searchCommunityByCommunityType(ctx *gin.Context) {
fmt.Println("Line no 63")

var req searchCommunityByCommunityTypeRequest
fmt.Println("Line no 66")
fmt.Println("CommunityType", req)
err := ctx.ShouldBindJSON(&req)
if err != nil {
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
}
fmt.Println("Line no 70")
fmt.Println("communityType: ", req.CommunityType)
profile, err := server.store.SearchCommunityByCommunityType(ctx, req.CommunityType)
if err != nil {
ctx.JSON(http.StatusNotFound, errorResponse(err))
return
}
fmt.Println("Line no 77")
fmt.Println("Profile: ", profile)
ctx.JSON(http.StatusOK, profile)
return
}
4 changes: 3 additions & 1 deletion api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ func NewServer(config util.Config, store *db.Store) (*Server, error) {
authRouter.PUT("/updateProfileFullName", server.updateFullName)
authRouter.PUT("/updateAvatarUrl", server.updateAvatarUrl)
authRouter.PUT("/updateProfileBio", server.updateBio)

authRouter.POST("/searchCommunityType", server.searchCommunityByCommunityType)
authRouter.POST("/searchByFullName", server.searchByFullName)
authRouter.POST("/searchByCommunityName", server.searchByCommunityName)
//handler := corsHandle.Handler(router)

server.router = router
Expand Down
13 changes: 13 additions & 0 deletions db/query/search.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- name: SearchByCommunityName :one
SELECT communities_name FROM communities
WHERE communities_name ILIKE $1;

-- name: SearchCommunityByCommunityType :many
SELECT communities_name FROM communities
WHERE community_type ILIKE $1
ORDER BY id;

-- name: SearchByFullName :many
SELECT full_name, owner FROM profile
WHERE full_name ILIKE $1
ORDER BY id;
85 changes: 85 additions & 0 deletions db/sqlc/search.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.