Skip to content

Commit

Permalink
Merge branch 'main' of github.com:Filip-Petruneac/library
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip-Petruneac committed Oct 2, 2024
2 parents fe6615a + 25b7daa commit 1d2c38e
Show file tree
Hide file tree
Showing 3 changed files with 2,003 additions and 1,560 deletions.
135 changes: 71 additions & 64 deletions api/coverage.out
Original file line number Diff line number Diff line change
Expand Up @@ -267,67 +267,74 @@ mymodule/main.go:1036.16,1040.3 3 0
mymodule/main.go:1042.2,1043.23 2 1
mymodule/main.go:1043.23,1046.3 2 0
mymodule/main.go:1048.2,1048.47 1 1
mymodule/main.go:1052.68,1053.63 1 1
mymodule/main.go:1053.63,1056.3 2 0
mymodule/main.go:1059.2,1061.16 3 1
mymodule/main.go:1061.16,1064.3 2 0
mymodule/main.go:1067.2,1075.16 3 1
mymodule/main.go:1075.16,1078.3 2 0
mymodule/main.go:1079.2,1082.44 2 1
mymodule/main.go:1082.44,1085.3 2 0
mymodule/main.go:1088.2,1096.16 3 1
mymodule/main.go:1096.16,1100.3 3 0
mymodule/main.go:1103.2,1104.23 2 1
mymodule/main.go:1104.23,1107.3 2 0
mymodule/main.go:1109.2,1109.45 1 1
mymodule/main.go:1113.74,1114.63 1 0
mymodule/main.go:1114.63,1117.3 2 0
mymodule/main.go:1120.2,1122.16 3 0
mymodule/main.go:1122.16,1125.3 2 0
mymodule/main.go:1128.2,1130.16 3 0
mymodule/main.go:1130.16,1133.3 2 0
mymodule/main.go:1134.2,1141.87 4 0
mymodule/main.go:1141.87,1144.3 2 0
mymodule/main.go:1147.2,1155.16 3 0
mymodule/main.go:1155.16,1159.3 3 0
mymodule/main.go:1162.2,1163.23 2 0
mymodule/main.go:1163.23,1166.3 2 0
mymodule/main.go:1168.2,1168.51 1 0
mymodule/main.go:1172.70,1173.35 1 0
mymodule/main.go:1173.35,1176.3 2 0
mymodule/main.go:1179.2,1181.16 3 0
mymodule/main.go:1181.16,1184.3 2 0
mymodule/main.go:1187.2,1196.16 4 0
mymodule/main.go:1196.16,1200.3 3 0
mymodule/main.go:1203.2,1203.18 1 0
mymodule/main.go:1203.18,1206.3 2 0
mymodule/main.go:1209.2,1216.16 3 0
mymodule/main.go:1216.16,1220.3 3 0
mymodule/main.go:1223.2,1224.23 2 0
mymodule/main.go:1224.23,1227.3 2 0
mymodule/main.go:1229.2,1229.47 1 0
mymodule/main.go:1233.68,1234.35 1 0
mymodule/main.go:1234.35,1237.3 2 0
mymodule/main.go:1240.2,1242.16 3 0
mymodule/main.go:1242.16,1245.3 2 0
mymodule/main.go:1248.2,1257.16 4 0
mymodule/main.go:1257.16,1261.3 3 0
mymodule/main.go:1264.2,1273.16 4 0
mymodule/main.go:1273.16,1277.3 3 0
mymodule/main.go:1280.2,1287.16 3 0
mymodule/main.go:1287.16,1291.3 3 0
mymodule/main.go:1294.2,1295.23 2 0
mymodule/main.go:1295.23,1298.3 2 0
mymodule/main.go:1301.2,1301.24 1 0
mymodule/main.go:1301.24,1309.17 3 0
mymodule/main.go:1309.17,1313.4 3 0
mymodule/main.go:1316.2,1316.45 1 0
mymodule/main.go:1320.74,1321.35 1 0
mymodule/main.go:1321.35,1324.3 2 0
mymodule/main.go:1327.2,1329.16 3 0
mymodule/main.go:1329.16,1332.3 2 0
mymodule/main.go:1335.2,1342.16 3 0
mymodule/main.go:1342.16,1346.3 3 0
mymodule/main.go:1349.2,1350.23 2 0
mymodule/main.go:1350.23,1353.3 2 0
mymodule/main.go:1355.2,1355.51 1 0
mymodule/main.go:1052.68,1053.66 1 1
mymodule/main.go:1053.66,1056.6 2 1
mymodule/main.go:1058.5,1059.19 2 1
mymodule/main.go:1059.19,1062.6 2 1
mymodule/main.go:1064.5,1065.65 2 1
mymodule/main.go:1065.65,1068.6 2 1
mymodule/main.go:1069.5,1071.50 2 1
mymodule/main.go:1071.50,1074.6 2 1
mymodule/main.go:1076.5,1083.19 3 1
mymodule/main.go:1083.19,1086.6 2 1
mymodule/main.go:1088.5,1089.19 2 1
mymodule/main.go:1089.19,1092.6 2 1
mymodule/main.go:1093.5,1093.26 1 1
mymodule/main.go:1093.26,1096.6 2 1
mymodule/main.go:1098.5,1098.97 1 1
mymodule/main.go:1103.74,1104.66 1 1
mymodule/main.go:1104.66,1107.6 2 1
mymodule/main.go:1109.5,1110.19 2 1
mymodule/main.go:1110.19,1113.6 2 1
mymodule/main.go:1115.5,1116.71 2 1
mymodule/main.go:1116.71,1119.6 2 1
mymodule/main.go:1120.5,1122.62 2 1
mymodule/main.go:1122.62,1125.6 2 1
mymodule/main.go:1127.5,1134.19 3 1
mymodule/main.go:1134.19,1137.6 2 1
mymodule/main.go:1139.5,1140.19 2 1
mymodule/main.go:1140.19,1143.6 2 1
mymodule/main.go:1144.5,1144.26 1 1
mymodule/main.go:1144.26,1147.6 2 1
mymodule/main.go:1149.5,1149.103 1 1
mymodule/main.go:1153.58,1154.90 1 1
mymodule/main.go:1154.90,1156.6 1 1
mymodule/main.go:1157.5,1157.15 1 1
mymodule/main.go:1162.70,1163.35 1 0
mymodule/main.go:1163.35,1166.3 2 0
mymodule/main.go:1169.2,1171.16 3 0
mymodule/main.go:1171.16,1174.3 2 0
mymodule/main.go:1177.2,1186.16 4 0
mymodule/main.go:1186.16,1190.3 3 0
mymodule/main.go:1193.2,1193.18 1 0
mymodule/main.go:1193.18,1196.3 2 0
mymodule/main.go:1199.2,1206.16 3 0
mymodule/main.go:1206.16,1210.3 3 0
mymodule/main.go:1213.2,1214.23 2 0
mymodule/main.go:1214.23,1217.3 2 0
mymodule/main.go:1219.2,1219.47 1 0
mymodule/main.go:1223.68,1224.35 1 0
mymodule/main.go:1224.35,1227.3 2 0
mymodule/main.go:1230.2,1232.16 3 0
mymodule/main.go:1232.16,1235.3 2 0
mymodule/main.go:1238.2,1247.16 4 0
mymodule/main.go:1247.16,1251.3 3 0
mymodule/main.go:1254.2,1263.16 4 0
mymodule/main.go:1263.16,1267.3 3 0
mymodule/main.go:1270.2,1277.16 3 0
mymodule/main.go:1277.16,1281.3 3 0
mymodule/main.go:1284.2,1285.23 2 0
mymodule/main.go:1285.23,1288.3 2 0
mymodule/main.go:1291.2,1291.24 1 0
mymodule/main.go:1291.24,1299.17 3 0
mymodule/main.go:1299.17,1303.4 3 0
mymodule/main.go:1306.2,1306.45 1 0
mymodule/main.go:1310.74,1311.35 1 0
mymodule/main.go:1311.35,1314.3 2 0
mymodule/main.go:1317.2,1319.16 3 0
mymodule/main.go:1319.16,1322.3 2 0
mymodule/main.go:1325.2,1332.16 3 0
mymodule/main.go:1332.16,1336.3 3 0
mymodule/main.go:1339.2,1340.23 2 0
mymodule/main.go:1340.23,1343.3 2 0
mymodule/main.go:1345.2,1345.51 1 0
204 changes: 97 additions & 107 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1046,126 +1046,116 @@ func (app *App) UpdateAuthor(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Author updated successfully")
}

// UpdateBook updates an existing book in the database
// UpdateBook handles the updating of an existing book in the database
func (app *App) UpdateBook(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut && r.Method != http.MethodPost {
http.Error(w, "Only PUT or POST methods are supported", http.StatusMethodNotAllowed)
return
}

// Extract the book ID from the URL path
vars := mux.Vars(r)
bookID, err := strconv.Atoi(vars["id"])
if err != nil {
http.Error(w, "Invalid book ID", http.StatusBadRequest)
return
}

// Parse the JSON data received from the request
var book struct {
Title string `json:"title"`
AuthorID int `json:"author_id"`
Photo string `json:"photo"`
Details string `json:"details"`
IsBorrowed bool `json:"is_borrowed"`
}
err = json.NewDecoder(r.Body).Decode(&book)
if err != nil {
http.Error(w, "Invalid JSON data", http.StatusBadRequest)
return
}
defer r.Body.Close()

// Check if all required fields are filled
if book.Title == "" || book.AuthorID == 0 {
http.Error(w, "Title and AuthorID are required fields", http.StatusBadRequest)
return
}

// Query to update the book
query := `
UPDATE books
SET title = ?, author_id = ?, photo = ?, details = ?, is_borrowed = ?
WHERE id = ?
`

// Execute the query
result, err := app.DB.Exec(query, book.Title, book.AuthorID, book.Photo, book.Details, book.IsBorrowed, bookID)
if err != nil {
app.Logger.Printf("Failed to update book: %v", err)
http.Error(w, fmt.Sprintf("Failed to update book: %v", err), http.StatusInternalServerError)
return
}

// Check if any row was actually updated
rowsAffected, _ := result.RowsAffected()
if rowsAffected == 0 {
http.Error(w, "Book not found", http.StatusNotFound)
return
}
if r.Method != http.MethodPut && r.Method != http.MethodPost {
HandleError(w, app.Logger, "Only PUT or POST methods are supported", nil, http.StatusMethodNotAllowed)
return
}

bookID, err := GetIDFromRequest(r, "id")
if err != nil {
HandleError(w, app.Logger, "Invalid book ID", err, http.StatusBadRequest)
return
}

var book Book
if err := json.NewDecoder(r.Body).Decode(&book); err != nil {
HandleError(w, app.Logger, "Invalid JSON data", err, http.StatusBadRequest)
return
}
defer r.Body.Close()

if err := ValidateBookData(book); err != nil {
HandleError(w, app.Logger, err.Error(), nil, http.StatusBadRequest)
return
}

query := `
UPDATE books
SET title = ?, author_id = ?, photo = ?, details = ?, is_borrowed = ?
WHERE id = ?
`

fmt.Fprintf(w, "Book updated successfully")
result, err := app.DB.Exec(query, book.Title, book.AuthorID, book.Photo, book.Details, book.IsBorrowed, bookID)
if err != nil {
HandleError(w, app.Logger, "Failed to update book", err, http.StatusInternalServerError)
return
}

rowsAffected, err := result.RowsAffected()
if err != nil {
HandleError(w, app.Logger, "Failed to retrieve affected rows", err, http.StatusInternalServerError)
return
}
if rowsAffected == 0 {
HandleError(w, app.Logger, "Book not found", nil, http.StatusNotFound)
return
}

RespondWithJSON(w, http.StatusOK, map[string]string{"message": "Book updated successfully"})
}


// UpdateSubscriber updates an existing subscriber in the database
func (app *App) UpdateSubscriber(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut && r.Method != http.MethodPost {
http.Error(w, "Only PUT or POST methods are supported", http.StatusMethodNotAllowed)
return
}

// Extract the subscriber ID from the URL path
vars := mux.Vars(r)
subscriberID, err := strconv.Atoi(vars["id"])
if err != nil {
http.Error(w, "Invalid subscriber ID", http.StatusBadRequest)
return
}

// Parse the JSON data received from the request
var subscriber Subscriber
err = json.NewDecoder(r.Body).Decode(&subscriber)
if err != nil {
http.Error(w, "Invalid JSON data", http.StatusBadRequest)
return
}
defer r.Body.Close()

// Log the subscriber ID and received data for update
app.Logger.Printf("Updating subscriber with ID: %d", subscriberID)
app.Logger.Printf("Received data: %+v", subscriber)

// Check if all required fields are filled
if subscriber.Firstname == "" || subscriber.Lastname == "" || subscriber.Email == "" {
http.Error(w, "Firstname, Lastname, and Email are required fields", http.StatusBadRequest)
return
}

// Query to update the subscriber
query := `
if r.Method != http.MethodPut && r.Method != http.MethodPost {
HandleError(w, app.Logger, "Only PUT or POST methods are supported", nil, http.StatusMethodNotAllowed)
return
}

subscriberID, err := GetIDFromRequest(r, "id")
if err != nil {
HandleError(w, app.Logger, "Invalid subscriber ID", err, http.StatusBadRequest)
return
}

var subscriber Subscriber
if err := json.NewDecoder(r.Body).Decode(&subscriber); err != nil {
HandleError(w, app.Logger, "Invalid JSON data", err, http.StatusBadRequest)
return
}
defer r.Body.Close()

if err := ValidateSubscriberData(subscriber); err != nil {
HandleError(w, app.Logger, err.Error(), nil, http.StatusBadRequest)
return
}

query := `
UPDATE subscribers
SET lastname = ?, firstname = ?, email = ?
SET lastname = ?, firstname = ?, email = ?
WHERE id = ?
`

// Execute the query
result, err := app.DB.Exec(query, subscriber.Lastname, subscriber.Firstname, subscriber.Email, subscriberID)
if err != nil {
app.Logger.Printf("Failed to update subscriber: %v", err)
http.Error(w, fmt.Sprintf("Failed to update subscriber: %v", err), http.StatusInternalServerError)
return
}

// Check if any row was actually updated
rowsAffected, _ := result.RowsAffected()
if rowsAffected == 0 {
http.Error(w, "Subscriber not found", http.StatusNotFound)
return
}
result, err := app.DB.Exec(query, subscriber.Lastname, subscriber.Firstname, subscriber.Email, subscriberID)
if err != nil {
HandleError(w, app.Logger, "Failed to update subscriber", err, http.StatusInternalServerError)
return
}

rowsAffected, err := result.RowsAffected()
if err != nil {
HandleError(w, app.Logger, "Failed to retrieve affected rows", err, http.StatusInternalServerError)
return
}
if rowsAffected == 0 {
HandleError(w, app.Logger, "Subscriber not found", nil, http.StatusNotFound)
return
}

RespondWithJSON(w, http.StatusOK, map[string]string{"message": "Subscriber updated successfully"})
}

fmt.Fprintf(w, "Subscriber updated successfully")
// ValidateSubscriberData checks if the required fields for a subscriber are present
func ValidateSubscriberData(subscriber Subscriber) error {
if subscriber.Firstname == "" || subscriber.Lastname == "" || subscriber.Email == "" {
return fmt.Errorf("firstname, lastname, and email are required fields")
}
return nil
}


// DeleteAuthor deletes an existing author from the database
func (app *App) DeleteAuthor(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
Expand Down
Loading

0 comments on commit 1d2c38e

Please sign in to comment.