Skip to content

Commit

Permalink
#Add GetFromByPost functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
JonyBepary committed Nov 8, 2023
1 parent f785313 commit f4d3ba2
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 25 deletions.
1 change: 1 addition & 0 deletions api/Insomnia_2023-11-08.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
func TestComments_Save_Get(t *testing.T) {
// Create a new Comments object with some data
cm := &Comment{

PostId: "1",
Id: "2",
Text: "This is a comment",
Content: "This is a comment",
}

// Call the Save method
Expand Down
66 changes: 66 additions & 0 deletions internal/model/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/SohelAhmedJoni/Awazz-Backend/internal/durable"
"github.com/syndtr/goleveldb/leveldb/util"
"google.golang.org/protobuf/proto"
)

Expand All @@ -26,6 +27,71 @@ func (cm *Comment) Get() error {
return nil
}

func GetFromByPost(cms *[]Comment, PostId string) error {
ldb, err := durable.LevelDBCreateDatabase("Database/", "NOSQL", "/")
if err != nil {
return nil
}
defer ldb.Close()

iter := ldb.NewIterator(util.BytesPrefix([]byte(fmt.Sprintf("comment-%v", PostId))), nil)

for iter.Next() {
var cm Comment
err = proto.Unmarshal(iter.Value(), &cm)
if err != nil {
return nil
}
*cms = append(*cms, cm)
}

iter.Release()
err = iter.Error()
if err != nil {
return err
}
return nil
}

func (cm *Comment) SavetoSQL(frag_num int64) error {
//mysql put
db, err := durable.CreateDatabase("Database/", "Common", fmt.Sprintf("Shard_%d.sqlite", frag_num))
if err != nil {
return err
}
sql := `CREATE TABLE IF NOT EXISTS Comment (
Id VARCHAR(250) PRIMARY KEY,
User VARCHAR(250) NOT NULL,
PostId VARCHAR(250) NOT NULL,
UserId VARCHAR(250) NOT NULL,
RepliedTo VARCHAR(250),
Content TEXT,
CreatedDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UpdatedDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
Likes INTEGER DEFAULT 0,
Replies INTEGER DEFAULT 0,
IsDeleted BOOLEAN DEFAULT 0,
IsUpdated BOOLEAN DEFAULT 0
)`
defer db.Close()
_, err = db.Exec(sql)
if err != nil {
return err
}
statement, err := db.Prepare("INSERT INTO Comment (Id,User,PostId,UserId,RepliedTo,Content,CreatedDate,UpdatedDate,Likes,Replies,IsDeleted,IsUpdated) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)")

if err != nil {
panic(err)
}
cm.IsDeleted = false
cm.IsUpdated = false
_, err = statement.Exec(cm.Id, cm.User, cm.PostId, cm.UserId, cm.RepliedTo, cm.Content, cm.CreatedDate, cm.UpdatedDate, cm.Likes, len(cm.Replies), cm.IsDeleted, cm.IsUpdated)
if err != nil {
panic(err)
}
return nil
}

func (cm *Comment) Save() error {
//leveldb put
db, err := durable.LevelDBCreateDatabase("Database/", "NOSQL", "/")
Expand Down
45 changes: 23 additions & 22 deletions internal/model/comments.pb.go

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

2 changes: 1 addition & 1 deletion internal/model/comments.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ message Comment{
string post_id = 3;
string user_id = 4;
string replied_to = 5;
string text = 6 ;
string content = 6 ;
int64 created_date = 7 ;
int64 updated_date = 8 ;
int64 likes = 9;
Expand Down
14 changes: 14 additions & 0 deletions pkg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,17 @@ func StringToShard(s string) int64 {
}
return n
}

func StringFullToShard(s string) int64 {
var n int64
if s == "" {
// UserId is missing, return 401
n = 0
} else {
for _, r := range []rune(s) {
n += int64(r)
}
n = n % 6
}
return n
}

0 comments on commit f4d3ba2

Please sign in to comment.