Skip to content

Commit

Permalink
#Minor Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JonyBepary committed Nov 8, 2023
1 parent b3c77e3 commit 012957b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 84 deletions.
56 changes: 33 additions & 23 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,13 @@ func delPost(c *gin.Context) {

var post model.Post
post.Id = c.Query("Id")
err := post.DeletePost(post.Id)
shard, err := durable.Fragmentation_Get(post.Id)
if err != nil {
c.JSON(500, gin.H{"error": "post does not exist"})
return
}

err = post.DeletePost(post.Id, shard)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
Expand Down Expand Up @@ -341,7 +347,11 @@ func savePost(c *gin.Context) {
c.JSON(500, gin.H{"error": err.Error()})
return
}
err = post.SavePost()
spew.Dump(post.Community)
shard := pkg.StringToShard(post.Community)
durable.Fragmentation_Add(shard, post.Id)
spew.Dump(shard)
err = post.SavePost(shard)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
Expand Down Expand Up @@ -406,8 +416,8 @@ func delPerson(c *gin.Context) {

p := model.Person{}
p.Id = c.Query("Id")
shard, TF := durable.Fragmentation_Get(p.Id)
if TF {
shard, err := durable.Fragmentation_Get(p.Id)
if err != nil {
c.JSON(500, gin.H{"error": "person does not exist"})
return
}
Expand Down Expand Up @@ -592,12 +602,12 @@ func delCommunity(c *gin.Context) {
}
var community model.Community
community.Id = c.Query("Id")
shard, TF := durable.Fragmentation_Get(community.Id)
if TF {
shard, err := durable.Fragmentation_Get(community.Id)
if err != nil {
c.JSON(500, gin.H{"error": "community does not exist"})
return
}
err := community.DeleteCommunity(community.Id, shard)
err = community.DeleteCommunity(community.Id, shard)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
Expand Down Expand Up @@ -713,13 +723,13 @@ func delInstance(c *gin.Context) {
}
var instance model.Instance
instance.Id = c.Query("Id")
shard, TF := durable.Fragmentation_Get(instance.Id)
if TF {
shard, err := durable.Fragmentation_Get(instance.Id)
if err != nil {
c.JSON(500, gin.H{"error": "instance does not exist"})
return
}

err := instance.DeleteInstance(instance.Id, shard)
err = instance.DeleteInstance(instance.Id, shard)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
Expand Down Expand Up @@ -945,8 +955,8 @@ func delMessage(c *gin.Context) {
return
}
ldb.Close()
shard, TF := durable.Fragmentation_Get(p.MsgId)
if TF {
shard, err := durable.Fragmentation_Get(p.MsgId)
if err != nil {
c.JSON(500, gin.H{"error": "message does not exist"})
return
}
Expand Down Expand Up @@ -1592,8 +1602,8 @@ func delLike(c *gin.Context) {
UserId: UserID,
EntityId: EntityId,
}
shard, TF := durable.Fragmentation_Get(p.EntityId)
if TF {
shard, err := durable.Fragmentation_Get(p.EntityId)
if err != nil {
c.JSON(500, gin.H{"error": "like does not exist"})
return
}
Expand All @@ -1620,15 +1630,15 @@ func getLIKESByUserId(c *gin.Context) {
// }
// Get the Entity ID from the form data
user_id := c.Query("UserId")
shard, TF := durable.Fragmentation_Get(user_id)
if TF {
shard, err := durable.Fragmentation_Get(user_id)
if err != nil {
c.JSON(500, gin.H{"error": "like does not exist"})
return
}

// Get the user ID from the session
likes := model.Likes{}
err := likes.GetByUserId(user_id, shard)
err = likes.GetByUserId(user_id, shard)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
Expand All @@ -1653,13 +1663,13 @@ func getLIKESByEntityId(c *gin.Context) {
entity_id := c.Query("EntityId")
// Get the user ID from the session
likes := model.Likes{}
shard, TF := durable.Fragmentation_Get(entity_id)
if TF {
shard, err := durable.Fragmentation_Get(entity_id)
if err != nil {
c.JSON(500, gin.H{"error": "like does not exist"})
return
}

err := likes.GetByEntityId(entity_id, shard)
err = likes.GetByEntityId(entity_id, shard)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
Expand Down Expand Up @@ -1690,13 +1700,13 @@ func setLikeByEntityId(c *gin.Context) {
user_id := c.Query("user_id")
// Get the user ID from the session
v := model.Like{EntityId: entity_id, UserId: user_id, CreatedAt: time.Now().Unix()}
shard, TF := durable.Fragmentation_Get(entity_id)
if TF {
shard, err := durable.Fragmentation_Get(entity_id)
if err != nil {
c.JSON(500, gin.H{"error": "like does not exist"})
return
}

err := v.SaveLikes(shard)
err = v.SaveLikes(shard)

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
Expand Down
12 changes: 6 additions & 6 deletions internal/durable/fragmentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,26 @@ func Fragmentation_Remove(key string) {
}

// // Get returns the shard for a given rowID.
func Fragmentation_Get(rowID string) (int64, bool) {
func Fragmentation_Get(rowID string) (int64, error) {
ldb, err := LevelDBCreateDatabase("Database/", "GlobalSchema", "/")
if err != nil {
log.Print(err)
return -1, false
return -1, fmt.Errorf("error in creating database")
}
defer ldb.Close()
shard, err := ldb.Get([]byte(rowID), nil)
if err != nil {
log.Print(err)
return -1, false
return -1, fmt.Errorf("error in getting shard")
}
// conver shard to int64
n, err := strconv.ParseInt(string(shard), 10, 64)
if err != nil {
log.Print(err)
return -1, false
return -1, fmt.Errorf("error in converting shard to int64")
}

defer ldb.Close()
return n, true
return n, nil
}

// // FRAGMENTATION TO FILE MARSHALJSON
Expand Down
3 changes: 2 additions & 1 deletion internal/model/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/SohelAhmedJoni/Awazz-Backend/internal/durable"
)

//create table
func (n *Notifications) SaveNotifications() error {
db, err := durable.CreateDatabase("./Database/", "Common", "Shard_0.sqlite")

Expand Down Expand Up @@ -47,7 +48,7 @@ INSERT INTO NOTIFICATIONS (Receiver,Title,Body,Source,Image,Sound,Time,Channel,P

return nil
}

/// read table
func (n *Notifications) GetNotifications(msgId string) error {
db, err := durable.CreateDatabase("./Database/", "Common", "Shard_0.sqlite")
if err != nil {
Expand Down
62 changes: 17 additions & 45 deletions internal/model/post.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package model

import (
"fmt"
"log"
"time"

"github.com/SohelAhmedJoni/Awazz-Backend/internal/durable"
)

func (p *Post) SavePost() error {
db, err := durable.CreateDatabase("./Database/", "Common", "Shard_0.sqlite")
func (p *Post) SavePost(frag_num int64) error {
db, err := durable.CreateDatabase("./Database/", "Common", fmt.Sprintf("Shard_%d.sqlite", frag_num))
if err != nil {
panic(err)
}
defer db.Close()

str := `
CREATE TABLE IF NOT EXISTS POST(
Id VARCHAR(255) PRIMARY KEY,
Expand Down Expand Up @@ -53,59 +55,29 @@ func (p *Post) SavePost() error {
if err != nil {
panic(err)
}
str2 := `INSERT INTO Post (Id,Community,Content,CreatedAt,UpdatedAt,DeletedAt,Likes,Shares,Comments,Author,Parent,Rank,IsSensitive,IsNsfw,IsDeleted,IsPinned,IsEdited,IsLiked,IsShared,IsCommented,IsSubscribed,IsBookmarked,IsReblogged,IsMentioned,IsPoll,IsPollVoted,IsPollExpired,IsPollClosed,IsPollMultiple,IsPollHideTotals,FragmentationKey) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)

str2 := `INSERT INTO POST (Id,Community,Content,CreatedAt,UpdatedAt,DeletedAt,Likes,Shares,Comments,Author,Parent,Rank,IsSensitive,IsNsfw,IsDeleted,IsPinned,IsEdited,IsLiked,IsShared,IsCommented,IsSubscribed,IsBookmarked,IsReblogged,IsMentioned,IsPoll,IsPollVoted,IsPollExpired,IsPollClosed,IsPollMultiple,IsPollHideTotals,FragmentationKey) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
`
statement, err := db.Prepare(str2)
if err != nil {
panic(err)
}
_, err = (statement.Exec(p.Id,
p.Community,
p.Content,
p.CreatedAt,
p.UpdatedAt,
p.DeletedAt,
p.Likes,
p.Shares,
p.Comments,
p.Author,
p.Parent,
p.Rank,
p.IsSensitive,
p.IsNsfw,
p.IsDeleted,
p.IsPinned,
p.IsEdited,
p.IsLiked,
p.IsShared,
p.IsCommented,
p.IsSubscribed,
p.IsBookmarked,
p.IsReblogged,
p.IsMentioned,
p.IsPoll,
p.IsPollVoted,
p.IsPollExpired,
p.IsPollClosed,
p.IsPollMultiple,
p.IsPollHideTotals,
p.FragmentationKey,
))
_, err = statement.Exec(p.Id, p.Community, p.Content, p.CreatedAt, p.UpdatedAt, p.DeletedAt, p.Likes, p.Shares, p.Comments, p.Author, p.Parent, p.Rank, p.IsSensitive, p.IsNsfw, p.IsDeleted, p.IsPinned, p.IsEdited, p.IsLiked, p.IsShared, p.IsCommented, p.IsSubscribed, p.IsBookmarked, p.IsReblogged, p.IsMentioned, p.IsPoll, p.IsPollVoted, p.IsPollExpired, p.IsPollClosed, p.IsPollMultiple, p.IsPollHideTotals, p.FragmentationKey)
if err != nil {
panic(err)
}

return nil
}
func (p *Post) GetPost(msgId string) error {
db, err := durable.CreateDatabase("Database/", "Common", "Shard_0.sqlite")
func (p *Post) GetPost(msgId string, frag_num int64) error {
db, err := durable.CreateDatabase("Database/", "Common", fmt.Sprintf("Shard_%d.sqlite", frag_num))
if err != nil {
panic(err)
}
defer db.Close()

// spew.Dump(rows)
rows, err := db.Query("SELECT * FROM ")
rows, err := db.Query("SELECT * FROM POST WHERE Id = ?", msgId)
if err != nil {
panic(err)
}
Expand All @@ -124,8 +96,8 @@ func (p *Post) GetPost(msgId string) error {

return nil
}
func (u *Post) UpdatePost(ID string) error {
db, err := durable.CreateDatabase("./Database/", "Common", "Shard_0.sqlite")
func (u *Post) UpdatePost(ID string, frag_num int64) error {
db, err := durable.CreateDatabase("./Database/", "Common", fmt.Sprintf("Shard_%d.sqlite", frag_num))
if err != nil {
panic(err)
}
Expand All @@ -140,19 +112,19 @@ func (u *Post) UpdatePost(ID string) error {

return nil
}
func (d *Post) DeletePost(Id string) error {
db, err := durable.CreateDatabase("./Database/", "Common", "Shard_0.sqlite")
func (d *Post) DeletePost(Id string, frag_num int64) error {
db, err := durable.CreateDatabase("./Database/", "Common", fmt.Sprintf("Shard_%d.sqlite", frag_num))
if err != nil {
panic(err)
}
defer db.Close()

_, err = db.Exec("DELETE FROM POST WHERE Id= ?", Id)
if err != nil {
panic(err)
}

d.UpdatedAt = time.Now().Unix()

if err != nil {
log.Fatal(err)
}
return nil
}
Loading

0 comments on commit 012957b

Please sign in to comment.