Skip to content

Commit

Permalink
Cleanup community service
Browse files Browse the repository at this point in the history
  • Loading branch information
piyushroshan committed Feb 17, 2024
1 parent 034b3b7 commit 617a00a
Show file tree
Hide file tree
Showing 21 changed files with 77 additions and 110 deletions.
8 changes: 4 additions & 4 deletions services/community/api/auth/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,22 @@ func ExtractTokenID(r *http.Request, db *gorm.DB) (uint32, error) {
tokenString := ExtractToken(r)
tokenJSON, err := json.Marshal(Token{Token: tokenString})
if err != nil {
log.Printf(err.Error())
log.Println(err)
return 0, err
}

resp, err := http.Post(tokenVerifyURL, "application/json",
bytes.NewBuffer(tokenJSON))
if err != nil {
log.Printf(err.Error())
log.Println(err)
return 0, err
}

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

Expand Down Expand Up @@ -121,7 +121,7 @@ func Pretty(data interface{}) {
return
}

fmt.Println(string(b))
log.Println(string(b))
}

//
2 changes: 1 addition & 1 deletion services/community/api/config/Initialize_mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (server *Server) InitializeMongo(DbDriver, DbUser string, DbPassword string
log.Fatal(err)
}

fmt.Println("Connected to MongoDB!")
log.Println("Connected to MongoDB!")

}

Expand Down
16 changes: 8 additions & 8 deletions services/community/api/controllers/coupon_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ package controllers

import (
"encoding/json"
"io/ioutil"
"io"
"log"
"net/http"
"fmt"

"crapi.proj/goservice/api/models"
"crapi.proj/goservice/api/responses"
Expand All @@ -29,7 +29,7 @@ import (
//@params ResponseWriter, Request
//Server have database connection
func (s *Server) AddNewCoupon(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
responses.ERROR(w, http.StatusBadRequest, err)
return
Expand All @@ -56,26 +56,26 @@ func (s *Server) AddNewCoupon(w http.ResponseWriter, r *http.Request) {
//@params ResponseWriter, Request
//Server have database connection
func (s *Server) ValidateCoupon(w http.ResponseWriter, r *http.Request) {

//coupon := models.CouponBody{}
var bsonMap bson.M

body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
responses.ERROR(w, http.StatusBadRequest, err)
fmt.Println("No payload for ValidateCoupon", body, err)
log.Println("No payload for ValidateCoupon", body, err)
return
}
err = json.Unmarshal(body, &bsonMap)
if err != nil {
responses.ERROR(w, http.StatusUnprocessableEntity, err)
fmt.Println("Failed to read json body", err)
log.Println("Failed to read json body", err)
return
}
couponData, err := models.ValidateCode(s.Client, s.DB, bsonMap)

if err != nil {
fmt.Println("Error fetching Coupon", couponData, err)
log.Println("Error fetching Coupon", couponData, err)
responses.JSON(w, http.StatusInternalServerError, err)
return
}
Expand Down
6 changes: 3 additions & 3 deletions services/community/api/controllers/post_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package controllers

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"strconv"

Expand All @@ -31,7 +31,7 @@ import (
//Server have database connection
func (s *Server) AddNewPost(w http.ResponseWriter, r *http.Request) {

body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
responses.ERROR(w, http.StatusBadRequest, err)
return
Expand Down Expand Up @@ -111,7 +111,7 @@ func (s *Server) GetPost(w http.ResponseWriter, r *http.Request) {
func (s *Server) Comment(w http.ResponseWriter, r *http.Request) {

vars := mux.Vars(r)
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
responses.ERROR(w, http.StatusBadRequest, err)
return
Expand Down
2 changes: 1 addition & 1 deletion services/community/api/models/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func CommentOnPost(client *mongo.Client, postComment Comments) (Post, error) {
//Add comment in post
updatePost.Comments = append(updatePost.Comments, comments)

update := bson.D{{"$set", bson.D{{"comments", updatePost.Comments}}}}
update := bson.D{{Key: "$set", Value: bson.D{{Key: "comments", Value: updatePost.Comments}}}}

collection := client.Database("crapi").Collection("post")

Expand Down
9 changes: 5 additions & 4 deletions services/community/api/models/coupon.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ package models
import (
"context"
"errors"
"fmt"
"html"
"log"
"strings"
"time"

"github.com/jinzhu/gorm"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

//Coupon
Expand Down Expand Up @@ -63,9 +64,9 @@ func SaveCoupon(client *mongo.Client, coupon Coupon) (Coupon, error) {
// Insert a single document
insertResult, err := collection.InsertOne(context.TODO(), coupon)
if err != nil {
fmt.Println(err)
log.Println(err)
}
fmt.Println("Inserted a single document: ", insertResult.InsertedID)
log.Println("Inserted a single document: ", insertResult.InsertedID)

return coupon, err
}
Expand Down
9 changes: 4 additions & 5 deletions services/community/api/models/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package models
import (
"context"
"errors"
"fmt"
"html"
"log"
"reflect"
Expand Down Expand Up @@ -84,7 +83,7 @@ func SavePost(client *mongo.Client, post Post) (Post, error) {
collection := client.Database("crapi").Collection("post")
_, err := collection.InsertOne(context.TODO(), post)
if err != nil {
fmt.Println(err)
log.Println(err)
}

return post, err
Expand All @@ -96,7 +95,7 @@ func GetPostByID(client *mongo.Client, ID string) (Post, error) {

//filter := bson.D{{"name", "Ash"}}
collection := client.Database("crapi").Collection("post")
filter := bson.D{{"id", ID}}
filter := bson.D{{Key: "id", Value: ID}}
err := collection.FindOne(context.TODO(), filter).Decode(&post)

return post, err
Expand All @@ -108,15 +107,15 @@ func FindAllPost(client *mongo.Client, offset int, limit int) ([]interface{}, er
post := []Post{}

options := options.Find()
options.SetSort(bson.D{{"_id", -1}})
options.SetSort(bson.D{{Key: "_id", Value: -1}})
options.SetLimit(int64(limit))
options.SetSkip(int64(offset * limit))
collection := client.Database("crapi").Collection("post")
cur, err := collection.Find(context.Background(), bson.D{}, options)
if err != nil {
log.Println(err)
}
fmt.Println(cur)
log.Println(cur)
objectType := reflect.TypeOf(post).Elem()
var list = make([]interface{}, 0)
defer cur.Close(context.Background())
Expand Down
18 changes: 14 additions & 4 deletions services/community/api/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ package models

import (
"errors"
"log"
"strings"
"time"

"github.com/badoux/checkmail"
"github.com/jinzhu/gorm"
"golang.org/x/crypto/bcrypt"

"encoding/base64"
)

Expand Down Expand Up @@ -109,18 +110,27 @@ func FindAuthorByEmail(email string, db *gorm.DB) (*uint64, error) {
//fetch id and number from for token user
row := db.Table("user_login").Where("email LIKE ?", email).Select("id,number").Row()

row.Scan(&id, &number)
err = row.Scan(&id, &number)
if err != nil {
log.Println("Error in FindAuthorByEmail", err)
}

autherID = id
//fetch name and picture from for token user
row1 := db.Table("user_details").Where("user_id = ?", id).Select("name, lo_get(picture)").Row()
row1.Scan(&name, &picture)
err = row1.Scan(&name, &picture)
if err != nil {
log.Println("Error in FindAuthorByEmail", err)
}
if len(picture) > 0 {
picurl = "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(picture)
}
nickname = name
row2 := db.Table("vehicle_details").Where("owner_id = ?", id).Select("uuid").Row()
row2.Scan(&uuid)
err = row2.Scan(&uuid)
if err != nil {
log.Println("Error in FindAuthorByEmail", err)
}
vehicleID = uuid
return number, err
}
10 changes: 5 additions & 5 deletions services/community/api/router/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package router

import (
"fmt"
"log"
"net/http"
"os"
"time"
Expand All @@ -29,10 +29,10 @@ import (

type Server config.Server

var controller = controllers.Server{}

// initializeRoutes initialize routes of url with Authentication or without Authentication
func (server *Server) InitializeRoutes() *mux.Router {
var controller = controllers.Server{}

controller.DB = server.DB

Expand All @@ -59,7 +59,7 @@ func (server *Server) InitializeRoutes() *mux.Router {
}

func (server *Server) Run(addr string) {
fmt.Println("Listening to port " + os.Getenv("SERVER_PORT"))
log.Println("Listening to port " + os.Getenv("SERVER_PORT"))
srv := &http.Server{
Addr: addr,
Handler: server.Router,
Expand All @@ -79,12 +79,12 @@ func (server *Server) Run(addr string) {
}
err := srv.ListenAndServeTLS(certificate, key)
if err != nil {
fmt.Println(err)
log.Println(err)
}
} else {
err := srv.ListenAndServe()
if err != nil {
fmt.Println(err)
log.Println(err)
}
}
}
23 changes: 15 additions & 8 deletions services/community/api/seed/seeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package seed

import (
"context"
"fmt"
"log"
"os"
"time"

Expand Down Expand Up @@ -70,20 +70,27 @@ func LoadMongoData(mongoClient *mongo.Client, db *gorm.DB) {
// get a MongoDB document using the FindOne() method
err := collection.FindOne(context.TODO(), bson.D{}).Decode(&couponResult)
if err != nil {
for i, _ := range coupons {
for i := range coupons {
couponData, err := collection.InsertOne(context.TODO(), coupons[i])
fmt.Println(couponData, err)
log.Println(couponData, err)
}
}
postCollection := mongoClient.Database(os.Getenv("MONGO_DB_NAME")).Collection("post")
er := postCollection.FindOne(context.TODO(), bson.D{}).Decode(&postResult)
if er != nil {
for j, _ := range posts {
models.FindAuthorByEmail(emails[j], db)
for j := range posts {
author, err := models.FindAuthorByEmail(emails[j], db)
if err != nil {
log.Println("Error finding author", err)
continue
}
log.Println(author)
posts[j].Prepare()
models.SavePost(mongoClient, posts[j])
//postData, err := collection.InsertOne(context.TODO(), posts[j])
//fmt.Println(postData, err)
postData, err := models.SavePost(mongoClient, posts[j]) // Assign the returned values to separate variables
if err != nil {
log.Println("Error saving post", err)
}
log.Println(postData) // Use the returned values as needed
}
}
}
5 changes: 3 additions & 2 deletions services/community/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ import (
"github.com/joho/godotenv"
)

var server = config.Server{}
var route = router.Server{}


func init() {
// loads values from .env into the system
Expand All @@ -36,6 +35,8 @@ func init() {
}

func Run() {
var server = config.Server{}
var route = router.Server{}

route.Client = server.InitializeMongo("mongodb", os.Getenv("MONGO_DB_USER"), os.Getenv("MONGO_DB_PASSWORD"), os.Getenv("MONGO_DB_PORT"), os.Getenv("MONGO_DB_HOST"))

Expand Down
3 changes: 0 additions & 3 deletions services/community/vendor/github.com/badoux/checkmail/go.mod

This file was deleted.

1 change: 0 additions & 1 deletion services/community/vendor/github.com/go-stack/stack/go.mod

This file was deleted.

1 change: 0 additions & 1 deletion services/community/vendor/github.com/golang/snappy/go.mod

This file was deleted.

1 change: 0 additions & 1 deletion services/community/vendor/github.com/google/uuid/go.mod

This file was deleted.

3 changes: 0 additions & 3 deletions services/community/vendor/github.com/gorilla/mux/go.mod

This file was deleted.

14 changes: 0 additions & 14 deletions services/community/vendor/github.com/jinzhu/gorm/go.mod

This file was deleted.

Loading

0 comments on commit 617a00a

Please sign in to comment.