Skip to content

Commit

Permalink
Pagination for UI (#249)
Browse files Browse the repository at this point in the history
* Pagination for UI
  • Loading branch information
piyushroshan authored May 17, 2024
1 parent b6e81a1 commit d2c1941
Show file tree
Hide file tree
Showing 67 changed files with 1,553 additions and 966 deletions.
2 changes: 1 addition & 1 deletion services/community/api/auth/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ func ExtractTokenID(r *http.Request, db *gorm.DB) (uint32, error) {

tokenValid := resp.StatusCode == 200
token, _, err := new(jwt.Parser).ParseUnverified(tokenString, jwt.MapClaims{})
claims, ok := token.Claims.(jwt.MapClaims)
if err != nil {
log.Println(err)
return 0, err
}
claims, ok := token.Claims.(jwt.MapClaims)

if ok && tokenValid {
name := claims["sub"]
Expand Down
10 changes: 6 additions & 4 deletions services/community/api/controllers/post_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,17 @@ func (s *Server) GetPostByID(w http.ResponseWriter, r *http.Request) {

}



//GetPost Vulnerabilities
func (s *Server) GetPost(w http.ResponseWriter, r *http.Request) {
//post := models.Post{}
limit_param := r.URL.Query().Get("limit")
limit := 30
var limit int64 = 30
err := error(nil)
if limit_param != "" {
// Parse limit_param and set to limit
limit, err = strconv.Atoi(limit_param)
limit, err = strconv.ParseInt(limit_param, 10, 64)
if err != nil {
limit = 30
}
Expand All @@ -88,10 +90,10 @@ func (s *Server) GetPost(w http.ResponseWriter, r *http.Request) {
limit = 50
}

offset := 0
var offset int64 = 0
offset_param := r.URL.Query().Get("offset")
if offset_param != "" {
offset, err = strconv.Atoi(offset_param)
offset, err = strconv.ParseInt(offset_param, 10, 64)
if err != nil {
offset = 0
}
Expand Down
9 changes: 4 additions & 5 deletions services/community/api/models/coupon.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,27 @@ func (c *Coupon) Prepare() {
func (c *Coupon) Validate() error {

if c.CouponCode == "" {
return errors.New("Required Coupon Code")
return errors.New("required coupon code")
}
if c.Amount == "" {
return errors.New("Required Coupon Amount")
return errors.New("required coupon amount")
}

return nil
}

//SaveCoupon save coupon database.
//SaveCoupon save coupon in database.
func SaveCoupon(client *mongo.Client, coupon Coupon) (Coupon, error) {

// Get a handle for your collection
collection := client.Database("crapi").Collection("coupon")
collection := client.Database("crapi").Collection("coupons")

// Insert a single document
insertResult, err := collection.InsertOne(context.TODO(), coupon)
if err != nil {
log.Println(err)
}
log.Println("Inserted a single document: ", insertResult.InsertedID)

return coupon, err
}

Expand Down
73 changes: 47 additions & 26 deletions services/community/api/models/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"errors"
"html"
"log"
"reflect"
"strings"
"time"

Expand Down Expand Up @@ -51,17 +50,24 @@ func (post *Post) Prepare() {
post.CreatedAt = time.Now()
}

type PostsResponse struct {
Posts []Post `json:"posts"`
NextOffset *int64 `json:"next_offset"`
PrevOffset *int64 `json:"previous_offset"`
Total int `json:"total"`
}

//Validate data of post
func (post *Post) Validate() error {

if post.Title == "" {
return errors.New("Required Title")
return errors.New("required title")
}
if post.Content == "" {
return errors.New("Required Content")
return errors.New("tequired content")
}
if post.AuthorID < 1 {
return errors.New("Required Author")
return errors.New("required author")
}
return nil
}
Expand Down Expand Up @@ -97,42 +103,57 @@ func GetPostByID(client *mongo.Client, ID string) (Post, error) {
collection := client.Database("crapi").Collection("post")
filter := bson.D{{Key: "id", Value: ID}}
err := collection.FindOne(context.TODO(), filter).Decode(&post)
if err != nil {
log.Println(err)
}

return post, err

}

//FindAllPost return all recent post
func FindAllPost(client *mongo.Client, offset int, limit int) ([]interface{}, error) {
post := []Post{}

func FindAllPost(client *mongo.Client, offset int64, limit int64) (PostsResponse, error) {
postList := []Post{}
var postsResponse PostsResponse = PostsResponse{}
options := options.Find()
options.SetSort(bson.D{{Key: "_id", Value: -1}})
options.SetLimit(int64(limit))
options.SetSkip(int64(offset * limit))
options.SetLimit(limit)
options.SetSkip(offset)
ctx := context.Background()
collection := client.Database("crapi").Collection("post")
cur, err := collection.Find(context.Background(), bson.D{}, options)
cur, err := collection.Find(ctx, bson.D{}, options)
if err != nil {
log.Println(err)
log.Println("Error in finding posts: ", err)
return postsResponse, err
}
log.Println(cur)
objectType := reflect.TypeOf(post).Elem()
var list = make([]interface{}, 0)
defer cur.Close(context.Background())
for cur.Next(context.Background()) {
result := reflect.New(objectType).Interface()
err := cur.Decode(result)

for cur.Next(ctx) {
var elem Post
err := cur.Decode(&elem)
if err != nil {
log.Println(err)
return nil, err
log.Println("Error in decoding posts: ", err)
return postsResponse, err
}
postList = append(postList, elem)
}

list = append(list, result)
postsResponse.Posts = postList
// get posts count for pagination
count, err1 := collection.CountDocuments(context.Background(), bson.D{})
if err1 != nil {
log.Println("Error in counting posts: ", err1)
return postsResponse, err1
}
if err := cur.Err(); err != nil {
return nil, err
if offset - limit >= 0 {
tempOffset := offset - limit
postsResponse.PrevOffset = &tempOffset
}

return list, err
if offset + limit < count {
tempOffset := offset + limit
postsResponse.NextOffset = &tempOffset
}
postsResponse.Total = len(postList)
if err = cur.Err(); err != nil {
log.Println("Error in cursor: ", err)
}
return postsResponse, err
}
18 changes: 9 additions & 9 deletions services/community/api/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,36 @@ func (u *Author) Validate(action string) error {
switch strings.ToLower(action) {
case "update":
if u.Nickname == "" {
return errors.New("Required Nickname")
return errors.New("required nickname")
}
if u.Email == "" {
return errors.New("Required Email")
return errors.New("required email")
}
if err := checkmail.ValidateFormat(u.Email); err != nil {
return errors.New("Invalid Email")
return errors.New("invalid email")
}
return nil

case "login":
if u.Nickname == "" {
return errors.New("Required Nickname")
return errors.New("required nickname")
}
if u.Email == "" {
return errors.New("Required Email")
return errors.New("required email")
}
if err := checkmail.ValidateFormat(u.Email); err != nil {
return errors.New("Invalid Email")
return errors.New("invalid email")
}
return nil
default:
if u.Nickname == "" {
return errors.New("Required Nickname")
return errors.New("required nickname")
}
if u.Email == "" {
return errors.New("Required Email")
return errors.New("required email")
}
if err := checkmail.ValidateFormat(u.Email); err != nil {
return errors.New("Invalid Email")
return errors.New("invalid email")
}
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion services/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "crapi-web",
"version": "0.1.0",
"private": true,
"proxy": "http://localhost:8889",
"dependencies": {
"@ant-design/icons": "^4.8.1",
"@testing-library/jest-dom": "^4.2.4",
Expand All @@ -28,7 +29,8 @@
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"lint": "prettier --check src/**/*.{js,jsx}"
"lint": "prettier --check src/**/*.{js,jsx}",
"lint:fix": "prettier --write src/**/*.{js,jsx}"
},
"eslintConfig": {
"extends": "react-app"
Expand Down
3 changes: 2 additions & 1 deletion services/web/src/actions/communityActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@

import actionTypes from "../constants/actionTypes";

export const getPostsAction = ({ accessToken, callback }) => {
export const getPostsAction = ({ accessToken, callback, ...data }) => {
return {
type: actionTypes.GET_POSTS,
accessToken,
callback,
...data,
};
};

Expand Down
6 changes: 4 additions & 2 deletions services/web/src/actions/shopActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@

import actionTypes from "../constants/actionTypes";

export const getProductsAction = ({ callback, accessToken }) => {
export const getProductsAction = ({ callback, accessToken, ...data }) => {
return {
type: actionTypes.GET_PRODUCTS,
accessToken,
callback,
...data,
};
};

Expand All @@ -32,11 +33,12 @@ export const buyProductAction = ({ callback, accessToken, ...data }) => {
};
};

export const getOrdersAction = ({ callback, accessToken }) => {
export const getOrdersAction = ({ callback, accessToken, ...data }) => {
return {
type: actionTypes.GET_ORDERS,
accessToken,
callback,
...data,
};
};

Expand Down
3 changes: 2 additions & 1 deletion services/web/src/actions/userActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,12 @@ export const resetPasswordAction = ({
};
};

export const getServicesAction = ({ callback, accessToken }) => {
export const getServicesAction = ({ callback, accessToken, ...data }) => {
return {
type: actionTypes.GET_SERVICES,
accessToken,
callback,
...data,
};
};

Expand Down
11 changes: 9 additions & 2 deletions services/web/src/actions/vehicleActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,27 @@ export const verifyVehicleAction = ({ callback, accessToken, ...data }) => {
};
};

export const getMechanicsAction = ({ callback, accessToken }) => {
export const getMechanicsAction = ({ callback, accessToken, ...data }) => {
return {
type: actionTypes.GET_MECHANICS,
accessToken,
callback,
...data,
};
};

export const getVehiclesAction = ({ callback, accessToken, email }) => {
export const getVehiclesAction = ({
callback,
accessToken,
email,
...data
}) => {
return {
type: actionTypes.GET_VEHICLES,
accessToken,
callback,
email,
...data,
};
};

Expand Down
Loading

0 comments on commit d2c1941

Please sign in to comment.