[PD1] Fix adding user cert to db, also finished logs

This commit is contained in:
Afonso Franco 2024-04-28 23:32:19 +01:00
parent f2118fe570
commit 4ea8315aed
Signed by: afonso
SSH key fingerprint: SHA256:aiLbdlPwXKJS5wMnghdtod0SPy8imZjlVvCyUX9DJNk
4 changed files with 24 additions and 16 deletions

View file

@ -4,7 +4,6 @@ import (
"PD1/internal/protocol"
"crypto/x509"
"database/sql"
"errors"
"fmt"
"log"
"time"
@ -91,7 +90,7 @@ func (ds DataStore) GetMessage(toUID string, position int) protocol.Packet {
err := row.Scan(&serverMessage.FromUID, &serverMessage.ToUID, &serverMessage.Subject, &serverMessage.Body, &serverMessage.Timestamp)
if err == sql.ErrNoRows {
log.Printf("No message with NUM %v for UID %v\n", position, toUID)
errorMessage := fmt.Sprintf("No message with NUM %v", position)
errorMessage := fmt.Sprintf("MSG SERVICE: unknown message!")
return protocol.NewReportErrorPacket(errorMessage)
}
@ -216,8 +215,8 @@ func (ds DataStore) userExists(uid string) bool {
var count int
// Execute the SQL query
err := ds.db.QueryRow(query, uid).Scan(&count)
if err == sql.ErrNoRows {
log.Println("user with UID %v does not exist", uid)
if err != nil || count == 0 {
log.Printf("user with UID %v does not exist\n", uid)
return false
}
return true
@ -236,7 +235,7 @@ func (ds DataStore) storeUserCertIfNotExists(uid string, cert x509.Certificate)
`
_, err := ds.db.Exec(insertQuery, uid, cert.Raw)
if err != nil {
return errors.New(fmt.Sprintf("Error storing user certificate for UID %s: %v\n", uid, err))
return fmt.Errorf("Error storing user certificate for UID %s: %v\n", uid, err)
}
log.Printf("User certificate for UID %s stored successfully.\n", uid)
return nil