Skip to content

Commit

Permalink
CRUD function of messages.go file
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorna-Das committed Oct 23, 2023
1 parent e93a4f4 commit 1916ac3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 41 deletions.
66 changes: 29 additions & 37 deletions internal/model/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -20,35 +19,35 @@ 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)
if err != nil {
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)
}
return nil
}

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)
}
Expand All @@ -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()

Expand All @@ -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 {
Expand All @@ -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
}
7 changes: 3 additions & 4 deletions internal/model/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1916ac3

Please sign in to comment.