Skip to content

Commit

Permalink
Format code with prettier and gofumpt
Browse files Browse the repository at this point in the history
This commit fixes the style issues introduced in 2723e7c according to the output
from prettier and gofumpt.

Details: https://deepsource.io/gh/1nwf/Produx-v2/transform/36decb60-4fb5-43ca-90c8-b5b468933488/
  • Loading branch information
deepsource-autofix[bot] committed Nov 12, 2022
1 parent 2723e7c commit 8333465
Show file tree
Hide file tree
Showing 25 changed files with 9,475 additions and 9,440 deletions.
26 changes: 16 additions & 10 deletions backend/controllers/pc/WSMsgHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (c *socketConnection) sendMessage(msg interface{}) error {
defer c.mu.Unlock()
return c.conn.WriteJSON(msg)
}

func (msg *Message) UnmarshalBinary(data []byte) error {
if err := json.Unmarshal(data, &msg); err != nil {
return err
Expand All @@ -53,15 +54,19 @@ func (msg *Message) MarshalBinary() ([]byte, error) {
return json.Marshal(msg)
}

var users = make(map[string][]*socketConnection)
var UserAccs = make(map[string]map[*websocket.Conn]*socketConnection)
var (
users = make(map[string][]*socketConnection)
UserAccs = make(map[string]map[*websocket.Conn]*socketConnection)
)

var ctx = context.Background()
var publisher = redis.NewClient(&redis.Options{
Addr: os.Getenv("REDIS_HOST") + ":6379", // use default Addr
Password: "", // no password set
DB: 0,
})
var (
ctx = context.Background()
publisher = redis.NewClient(&redis.Options{
Addr: os.Getenv("REDIS_HOST") + ":6379", // use default Addr
Password: "", // no password set
DB: 0,
})
)

func WSMsgHandler(ws *websocket.Conn, msg string) {
if conn, ok := UserAccs[ws.Params("id")][ws]; ok {
Expand Down Expand Up @@ -90,7 +95,7 @@ func WSMsgHandler(ws *websocket.Conn, msg string) {
Name: UserInfo.Name,
Pfp: UserInfo.Pfp,
}
var connection = &socketConnection{
connection := &socketConnection{
conn: ws,
user: user,
}
Expand Down Expand Up @@ -123,6 +128,7 @@ func publishMessage(conn *socketConnection, msg string) {
panic(err)
}
}

func SendMessage(msg Message) {
for _, client := range users[msg.ProductId] {
err := client.sendMessage(msg)
Expand All @@ -131,6 +137,7 @@ func SendMessage(msg Message) {
}
}
}

func makeMessage(conn *socketConnection, msgType messageType, msg string) *Message {
return &Message{
User: conn.user,
Expand All @@ -155,7 +162,6 @@ func HandleDisconnect(conn *socketConnection) {
delete(UserAccs[conn.conn.Params("id")], conn.conn)
}
SendUsersList(productId)

}

func SendUsersList(productId string) {
Expand Down
3 changes: 2 additions & 1 deletion backend/controllers/pc/delete/DeleteProduct.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package delete

import (
"tutorial/db"

"github.com/gofiber/fiber/v2"
"github.com/golang-jwt/jwt/v4"
"tutorial/db"
)

func DeleteProduct(c *fiber.Ctx) error {
Expand Down
1 change: 0 additions & 1 deletion backend/controllers/pc/get/GetPosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ func GetPosts(c *fiber.Ctx) error {
lastPost := db.DB.GetOldestPost(productIdInt, field)
return c.JSON(fiber.Map{"lastId": posts[len(posts)-1].GetID(), "posts": posts, "hasMore": lastPost.ID != posts[len(posts)-1].ID})
// }

}
3 changes: 2 additions & 1 deletion backend/controllers/pc/get/GetProducts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package get
import (
"database/sql"
"fmt"
"github.com/gofiber/fiber/v2"
"log"
"strconv"
"strings"
"tutorial/controllers/pc/mw"
"tutorial/db"

"github.com/gofiber/fiber/v2"
)

type ProductUsersInfo struct {
Expand Down
4 changes: 2 additions & 2 deletions backend/controllers/pc/get/IsProductFollowed.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package get

import (
"errors"
"tutorial/db"

"github.com/gofiber/fiber/v2"
"github.com/golang-jwt/jwt/v4"
"gorm.io/gorm"
"tutorial/db"
)

func IsProductFollowed(c *fiber.Ctx) error {
Expand All @@ -28,5 +29,4 @@ func IsProductFollowed(c *fiber.Ctx) error {
return c.Status(200).JSON(fiber.Map{"followed": false})
}
return c.Status(200).JSON(fiber.Map{"followed": true})

}
1 change: 0 additions & 1 deletion backend/controllers/pc/get/ProductDayStats.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ func ProductDayStats(c *fiber.Ctx) error {
"Working on ": workingOnCount,
"Done": doneCount,
})

}
1 change: 1 addition & 0 deletions backend/controllers/pc/patch/AddPostToBoard.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func hasSecondaryTypes(types pq.StringArray) (bool, string) {
}
return false, ""
}

func AddPostToBoard(c *fiber.Ctx) error {
productId := c.Params("productId")
postId := c.Params("postId")
Expand Down
40 changes: 20 additions & 20 deletions backend/controllers/pc/patch/RemovePostFromBoard.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
package patch

import (
"strings"
"tutorial/db"

"github.com/gofiber/fiber/v2"
"strings"
"tutorial/db"
"github.com/golang-jwt/jwt/v4"
)


func isPrimaryType(t string) bool {
switch t{
case "bugs":
case "suggestions":
case "announcements":
return true
switch t {
case "bugs":
case "suggestions":
case "announcements":
return true
}
return false
}
return false
}

func RemovePostFromBoard(c *fiber.Ctx) error {
productId := c.Params("productId")
postId := c.Params("postId")
field := strings.ToLower(c.Params("field"))
if !db.ValidType(field){
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"message": "invalid type",
})
}
if !db.ValidType(field) {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"message": "invalid type",
})
}
var product db.Product
var post db.Post
userToken := c.Locals("user").(*jwt.Token)
Expand All @@ -38,11 +39,10 @@ func RemovePostFromBoard(c *fiber.Ctx) error {
}
db.DB.First(&post, postId)

if isPrimaryType(field) {
return c.SendStatus(fiber.StatusBadRequest)
}
post.RemoveType(field)
if isPrimaryType(field) {
return c.SendStatus(fiber.StatusBadRequest)
}
post.RemoveType(field)


return c.Status(200).JSON(post)
}
38 changes: 19 additions & 19 deletions backend/controllers/pc/post/CreatePost.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,29 @@ func CreatePost(c *fiber.Ctx) error {
if err := c.BodyParser(newField); err != nil {
return c.Status(400).JSON(fiber.Map{"error": err.Error()})
}
postId := uuid.NewString()
err := os.Mkdir("./public/"+userInfo.Name+"/"+postId, 0755)
if err != nil{
return c.SendStatus(fiber.StatusInternalServerError)
}
postId := uuid.NewString()
err := os.Mkdir("./public/"+userInfo.Name+"/"+postId, 0755)
if err != nil {
return c.SendStatus(fiber.StatusInternalServerError)
}

for i := 1; i <= 3; i++ {
file, err := c.FormFile(fmt.Sprint("image", i))
if err != nil{
break
}
imageUrl := fmt.Sprintf("public/%s/%s/%s", userInfo.Name, postId,file.Filename)
savePath := fmt.Sprintf("./%s", imageUrl)
err = c.SaveFile(file, savePath)
if err != nil{
c.SendStatus(fiber.StatusInternalServerError)
}
newField.Images = append(newField.Images, url+ imageUrl)
}
for i := 1; i <= 3; i++ {
file, err := c.FormFile(fmt.Sprint("image", i))
if err != nil {
break
}
imageUrl := fmt.Sprintf("public/%s/%s/%s", userInfo.Name, postId, file.Filename)
savePath := fmt.Sprintf("./%s", imageUrl)
err = c.SaveFile(file, savePath)
if err != nil {
c.SendStatus(fiber.StatusInternalServerError)
}
newField.Images = append(newField.Images, url+imageUrl)
}

newField.UserID = uint(id)
newField.ProductID = product.ID
newField.Type = pq.StringArray{field}
newField.Type = pq.StringArray{field}
db.DB.Create(newField)
return c.Status(200).JSON(newField)
}
1 change: 0 additions & 1 deletion backend/controllers/pc/post/DislikeProduct.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ func DislikeProduct(c *fiber.Ctx) error {
return c.Status(200).JSON(fiber.Map{
"message": "Product Unliked",
})

}
1 change: 0 additions & 1 deletion backend/controllers/pc/post/LikeProduct.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ func LikeProduct(c *fiber.Ctx) error {
return c.Status(200).JSON(fiber.Map{
"message": "Product liked",
})

}
1 change: 0 additions & 1 deletion backend/controllers/uc/get/GetUserInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

func GetUserInfo(c *fiber.Ctx) error {

user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
id := claims["id"].(float64)
Expand Down
2 changes: 1 addition & 1 deletion backend/controllers/uc/get/GetUserProducts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/gofiber/fiber/v2"
)

//var DB = controllers.DB
// var DB = controllers.DB

func GetUserProducts(c *fiber.Ctx) error {
name := c.Params("name")
Expand Down
1 change: 0 additions & 1 deletion backend/controllers/uc/patch/UpdateUser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
)

func UpdateUser(c *fiber.Ctx) error {

user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
userId := claims["id"].(float64)
Expand Down
2 changes: 1 addition & 1 deletion backend/controllers/uc/post/Login.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"golang.org/x/crypto/bcrypt"
)

//var DB = controllers.DB
// var DB = controllers.DB

type loginInfo struct {
Username string `json:"username"`
Expand Down
3 changes: 3 additions & 0 deletions backend/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func (db *DBConn) GetLastPostCommentID(postId uint64) uint {
db.DB.Find(&comment, query)
return comment.ID
}

func (db *DBConn) GetFollowedProductsPosts(User User, field string, lastId uint64) ([]Post, error) {
if !ValidType(field) {
return nil, errors.New("invalid field")
Expand All @@ -153,6 +154,7 @@ func (db *DBConn) GetFollowedProductsPosts(User User, field string, lastId uint6
}
return posts, nil
}

func (db *DBConn) GetOldestFollowedProductsPost(UserFollowedProducts []Product, field string, lastId uint64) uint {
var productIds []uint
for _, product := range UserFollowedProducts {
Expand All @@ -172,6 +174,7 @@ func (db *DBConn) GetChatMessages(productId, lastId uint64) []Message {
}
return msgs
}

func (db *DBConn) GetLastProductMessageId(productId uint64) uint {
var msg Message
db.DB.Find(&msg, "id = (SELECT MIN(id) from messages where productId = ?)", productId)
Expand Down
4 changes: 4 additions & 0 deletions backend/db/posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (p *Post) AddType(t string) error {
DB.Save(&p)
return nil
}

func (p *Post) RemoveType(t string) error {
if !ValidType(t) {
return errors.New("invalid type")
Expand Down Expand Up @@ -137,6 +138,7 @@ func (p *Post) Delete() {
func (p *Post) GetID() uint {
return p.ID
}

func (c *Comment) BeforeCreate(tx *gorm.DB) error {
var post Post
err := DB.First(&post, c.PostID).Error
Expand All @@ -161,6 +163,7 @@ func (p *Post) Like(user User) error {
}
return nil
}

func (p *Post) Dislike(user User) error {
err := p.RemoveLike(user)
if err != nil {
Expand All @@ -174,6 +177,7 @@ func (p *Post) Dislike(user User) error {
}
return nil
}

func (p *Post) RemoveLike(user User) error {
err := DB.Model(&p).Association("UserLikes").Delete(&user)
DB.Table("liked_posts").Where("post_id = ? AND user_id = ?", p.ID, user.ID).Count(&p.Likes)
Expand Down
3 changes: 2 additions & 1 deletion backend/db/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (p *Product) BeforeCreate(tx *gorm.DB) error {
p.TSV = tsv
return nil
}

func (p *Product) BeforeUpdate(tx *gorm.DB) error {
p.Name = strings.Trim(p.Name, " ")
tsv, err := createTSVector(p.Name, p.Description)
Expand All @@ -45,10 +46,10 @@ func (p *Product) BeforeUpdate(tx *gorm.DB) error {
p.TSV = tsv
return nil
}

func createTSVector(name, description string) (string, error) {
query := fmt.Sprintf("select setweight(to_tsvector('english', '%s'), 'A') || setweight(to_tsvector('english', '%s'), 'B')", name, description)
rows, err := DB.Raw(query).Rows()

if err != nil {
return "", err
}
Expand Down
3 changes: 3 additions & 0 deletions backend/db/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,21 @@ func (user *User) RemoveLike(post *Post) {
}
DB.Save(&user)
}

func (user *User) RemoveDislike(post *Post) {
err := DB.Model(user).Association("DislikedPosts").Delete(post)
if err != nil {
panic(err)
}
DB.Save(&user)
}

func (user *User) DislikePost(post *Post) {
user.RemoveLike(post)
user.DislikedPosts = append(user.DislikedPosts, *post)
DB.Save(&user)
}

func (user *User) Update(name, email, password, pfp string) error {
if name != "" {
user.Name = name
Expand Down
Loading

0 comments on commit 8333465

Please sign in to comment.