diff --git a/internal/model/messages.go b/internal/model/messages.go index 1429c6d..f39f33b 100644 --- a/internal/model/messages.go +++ b/internal/model/messages.go @@ -6,11 +6,10 @@ import ( "time" "github.com/SohelAhmedJoni/Awazz-Backend/internal/durable" - "google.golang.org/protobuf/types/known/timestamppb" ) func (msg *Messages) SaveMessages() error { - db, err := durable.CreateDatabase("Database/messages.sqlite") + db, err := durable.CreateDatabase("./Database/", "common", "message.sqlite") if err != nil { panic(err) } @@ -20,13 +19,13 @@ func (msg *Messages) SaveMessages() error { MsgId VARCHAR(128) PRIMARY KEY, SenderId VARCHAR(128), ReceiverId VARCHAR(128), - Content TEXT, - SentTime TEXT, - LastEdit TEXT, + Content INTEGER, + SentTime INTEGER, + LastEdit INTEGER, DeleteTime TEXT, Status BOOL, - Attachment BLOB, - Type TEXT, + Attachment TEXT, + Types TEXT, Reaction TEXT) ` _, err = db.Exec(str) @@ -34,13 +33,13 @@ func (msg *Messages) SaveMessages() error { panic(err) } - statement, err := db.Prepare("INSERT INTO Messages (MsgId,SenderId,ReceiverId,Content,SentTime,LastEdit,DeleteTime,Status,Type,Reaction) VALUES (?,?,?,?,?,?,?,?,?,?)") + statement, err := db.Prepare("INSERT INTO Messages (MsgId,SenderId,ReceiverId,Content,SentTime,LastEdit,DeleteTime,Status,Attachment,Types,Reaction) VALUES (?,?,?,?,?,?,?,?,?,?,?)") if err != nil { panic(err) } - _, err = statement.Exec(msg.MsgId, msg.SenderId, msg.ReceiverId, msg.Content, msg.SentTime.String(), msg.LastEdit.String(), msg.DeleteTime.String(), msg.Status, msg.Types, msg.Reaction) + _, err = statement.Exec(msg.MsgId, msg.SenderId, msg.ReceiverId, msg.Content, msg.SentTime, msg.LastEdit, msg.DeleteTime, msg.Status, msg.Attachment, msg.Types, msg.Reaction) if err != nil { panic(err) } @@ -48,7 +47,7 @@ func (msg *Messages) SaveMessages() error { } func (m *Messages) GetMessages(msgId string) error { - db, err := durable.CreateDatabase("Database/messages.sqlite") + db, err := durable.CreateDatabase("./Database/", "common", "message.sqlite") if err != nil { panic(err) } @@ -61,36 +60,12 @@ func (m *Messages) GetMessages(msgId string) error { defer rows.Close() for rows.Next() { - SentTime := "" - LastEdit := "" - DeleteTime := "" - err = rows.Scan(&m.MsgId, &m.SenderId, &m.ReceiverId, &m.Content, &SentTime, &LastEdit, &DeleteTime, &m.Status, &m.Attachment, &m.Types, &m.Reaction) + err = rows.Scan(&m.MsgId, &m.SenderId, &m.ReceiverId, &m.Content, &m.SentTime, &m.LastEdit, &m.DeleteTime, &m.Status, &m.Attachment, &m.Types, &m.Reaction) if err != nil { log.Fatal(err) } - t, err := time.Parse(time.RFC3339, SentTime) - if err != nil { - panic(err) - } - - m.SentTime = timestamppb.New(t) - - t, err = time.Parse(time.RFC3339, LastEdit) - if err != nil { - panic(err) - } - - m.LastEdit = timestamppb.New(t) - - t, err = time.Parse(time.RFC3339, DeleteTime) - if err != nil { - panic(err) - } - - m.DeleteTime = timestamppb.New(t) - } err = rows.Err() @@ -102,12 +77,12 @@ func (m *Messages) GetMessages(msgId string) error { } func (u *Messages) UpdatedMessages(msgId string) error { - db, err := durable.CreateDatabase("Database/messages.sqlite") + db, err := durable.CreateDatabase("./Database/", "common", "message.sqlite") if err != nil { panic(err) } defer db.Close() - u.LastEdit = timestamppb.Now() + u.LastEdit = time.Now().Unix() _, err = db.Exec("UPDATE Messages SET LastEdit= ?, Content = ? WHERE msId = ? ", u.LastEdit, u.Content, msgId) if err != nil { @@ -116,3 +91,20 @@ func (u *Messages) UpdatedMessages(msgId string) error { return nil } + +func (d *Messages) DeleteMessages(msgId string) error { + db, err := durable.CreateDatabase("./Database/", "common", "message.sqlite") + if err != nil { + panic(err) + } + defer db.Close() + + _, err = db.Exec("DELETE FROM Messages WHERE msgId= ?", msgId) + + d.LastEdit = time.Now().Unix() + + if err != nil { + log.Fatal(err) + } + return nil +} diff --git a/internal/model/messages.proto b/internal/model/messages.proto index 3b4ad49..344682d 100644 --- a/internal/model/messages.proto +++ b/internal/model/messages.proto @@ -2,16 +2,15 @@ syntax = "proto3"; package model; option go_package = ".;model"; -import "google/protobuf/timestamp.proto"; message Messages{ string msg_id = 1; string sender_id = 2; string receiver_id = 3; string content = 4 ; - google.protobuf.Timestamp sent_time = 5 ; - google.protobuf.Timestamp last_edit = 6; - google.protobuf.Timestamp delete_time = 7; + int64 sent_time = 5 ; + int64 last_edit = 6; + int64 delete_time = 7; bool status = 8 ; string attachment = 9 ; string types = 10;