Skip to content

Commit

Permalink
removed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulqaharu committed May 5, 2024
1 parent 855f211 commit 09f103f
Showing 1 changed file with 1 addition and 10 deletions.
11 changes: 1 addition & 10 deletions backend/routes/quests.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ func SendChat(w http.ResponseWriter, r *http.Request) {
return
}

// Parse request body
var chat FactionChat
err := json.NewDecoder(r.Body).Decode(&chat)
if err != nil {
Expand All @@ -135,7 +134,6 @@ func SendChat(w http.ResponseWriter, r *http.Request) {
}
defer r.Body.Close()

// Insert chat into database
err = core.PostgresExec( r.Context(), "INSERT INTO FactionChats (sender, faction_key, message) VALUES ($1, $2, $3)", chat.Sender, chat.FactionKey, chat.Message)
if err != nil {
WriteErrorJson(w, http.StatusInternalServerError, "Failed to send chat")
Expand All @@ -152,7 +150,6 @@ func DeleteChat(w http.ResponseWriter, r *http.Request) {
return
}

// Parse query params
chatID := r.URL.Query().Get("id")
if chatID == "" {
WriteErrorJson(w, http.StatusBadRequest, "Missing chat ID parameter")
Expand All @@ -161,7 +158,6 @@ func DeleteChat(w http.ResponseWriter, r *http.Request) {

canDelete := false

// Retrieve the faction leader from the database
leader, err := core.PostgresQueryOne[string]("SELECT ftns.leader FROM Factions ftns INNER JOIN FactionChats ftcs ON ftns.faction_key = ftcs.key WHERE ftcs.id = $1", chatID)
if err != nil {
http.Error(w, "Failed to retrieve faction leader", http.StatusInternalServerError)
Expand All @@ -178,21 +174,18 @@ func DeleteChat(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Failed to retrieve sender", http.StatusInternalServerError)
return
}

if *sender == userAddress {
canDelete = true
}

}


// Check if the authenticated user is the sender or the faction leader
if !canDelete {
http.Error(w, "Not authorized to delete this chat", http.StatusForbidden)
return
}

// Delete chat from database
err = core.PostgresExec(r.Context(), "DELETE FROM FactionChats WHERE id = $1", chatID)
if err != nil {
WriteErrorJson(w, http.StatusInternalServerError, "Failed to delete chat")
Expand All @@ -208,14 +201,12 @@ func GetFactionChats(w http.ResponseWriter, r *http.Request) {
return
}

// Parse query params
factionKey := r.URL.Query().Get("faction_key")
if factionKey == "" {
WriteErrorJson(w, http.StatusBadRequest, "Missing faction key parameter")
return
}

// Retrieve faction chats from database
chats, err := core.PostgresQueryJson[FactionChat]("SELECT id, sender, faction_key, message, time FROM FactionChats WHERE faction_key = $1 ORDER BY time ASC", factionKey)
if err != nil {
WriteErrorJson(w, http.StatusInternalServerError, "Failed to get faction chats")
Expand Down

0 comments on commit 09f103f

Please sign in to comment.