Matrix/entities/room/roomDatabaseConnector.go

215 lines
3.4 KiB
Go

package room
import (
"fmt"
"nutfactory.org/Matrix/entities/event"
"nutfactory.org/Matrix/utils/database"
)
func CreateRoom(room *Room, userId string) (err error) {
sqlStmt := fmt.Sprintf(`INSERT INTO room
(id, version)
VALUES
(?, ?)`)
tx, err := database.DB.Begin()
if err != nil {
return
}
stmt, err := tx.Prepare(sqlStmt)
if err != nil {
return
}
defer stmt.Close()
_, err = stmt.Exec(room.Id, room.Version)
if err != nil {
return
}
tx.Commit()
err = CreateRoomMember(room.Id, userId)
return
}
func CreateRoomMember(roomId string, userId string) (err error) {
sqlStmt := fmt.Sprintf(`INSERT INTO roomMember
(roomId, userId)
VALUES
(?, ?)`)
tx, err := database.DB.Begin()
if err != nil {
return
}
stmt, err := tx.Prepare(sqlStmt)
if err != nil {
return
}
defer stmt.Close()
_, err = stmt.Exec(roomId, userId)
if err != nil {
return
}
tx.Commit()
return
}
func ReadRoom(id string) (foundRoom *Room, err error) {
queryStmt := fmt.Sprintf(`SELECT id, version
FROM room
WHERE id = '%s'`, id)
rows, err := database.DB.Query(queryStmt)
if err != nil {
return
}
defer rows.Close()
if rows.Next() {
foundRoom = &Room{}
err = rows.Scan(&foundRoom.Id, &foundRoom.Version)
if err != nil {
return
}
foundRoom.Messages, err = event.ReadEventsFromRoom(foundRoom.Id)
if err != nil {
return
}
foundRoom.Members, err = ReadRoomMembers(foundRoom.Id)
if err != nil {
return
}
}
return
}
func ReadRoomMembers(roomId string) (roomMembers []string, err error) {
queryStmt := fmt.Sprintf(`SELECT userId
FROM roomMember
WHERE roomId = '%s'`, roomId)
rows, err := database.DB.Query(queryStmt)
if err != nil {
return
}
defer rows.Close()
roomMembers = []string{}
for rows.Next() {
var foundUser string
err = rows.Scan(&foundUser)
if err != nil {
return
}
roomMembers = append(roomMembers, foundUser)
}
return
}
func UpdateRoom(room *Room) (err error) {
sqlStmt := fmt.Sprintf(`UPDATE room SET
version = ?
WHERE id = ?`)
tx, err := database.DB.Begin()
if err != nil {
return
}
stmt, err := tx.Prepare(sqlStmt)
if err != nil {
return
}
defer stmt.Close()
_, err = stmt.Exec(room.Version, room.Id)
if err != nil {
return
}
tx.Commit()
return
}
func DeleteRoom(id string) (err error) {
queryStmt := fmt.Sprintf(`DELETE FROM room
WHERE id = '%s'`, id)
tx, err := database.DB.Begin()
if err != nil {
return
}
_, err = database.DB.Exec(queryStmt)
if err != nil {
return
}
err = DeleteAllRoomMemberForRoom(id)
tx.Commit()
return
}
func DeleteRoomMember(roomId string, userId string) (err error) {
queryStmt := fmt.Sprintf(`DELETE FROM roomMember
WHERE userId = '%s' AND roomId = '%s'`, userId, roomId)
tx, err := database.DB.Begin()
if err != nil {
return
}
_, err = database.DB.Exec(queryStmt)
if err != nil {
return
}
tx.Commit()
return
}
func DeleteAllRoomMemberForUser(userId string) (err error) {
queryStmt := fmt.Sprintf(`DELETE FROM roomMember
WHERE userId = '%s'`, userId)
tx, err := database.DB.Begin()
if err != nil {
return
}
_, err = database.DB.Exec(queryStmt)
if err != nil {
return
}
tx.Commit()
return
}
func DeleteAllRoomMemberForRoom(roomId string) (err error) {
queryStmt := fmt.Sprintf(`DELETE FROM roomMember
WHERE roomId = '%s'`, roomId)
tx, err := database.DB.Begin()
if err != nil {
return
}
_, err = database.DB.Exec(queryStmt)
if err != nil {
return
}
tx.Commit()
return
}