Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atendees table db #37

Merged
merged 6 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions internal/database/atendee_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package eventsdb

import (
"main.go/models"

_ "github.com/go-sql-driver/mysql"
)
// Create attendee inserts a new attendee into the attendees table
func CreateAttendee(attendee models.Attendee) (*models.Attendee, error) {
insertQuery := `
INSERT INTO attendees (first_name, last_name, email, phone_number, image_url)
VALUES (?, ?, ?, ?, ?);`
_, err := dbmap.Exec(insertQuery, attendee.FirstName, attendee.LastName, attendee.Email, attendee.PhoneNumber, attendee.ImageURL)
if err != nil {
return nil, err
}

// Retrieve the newly inserted attendee
attendeeRow, err := dbmap.Query(`
SELECT attendee_id, first_name, last_name, email, phone_number, image_url
FROM attendees ORDER BY attendee_id DESC LIMIT 1`)
if err != nil {
return nil, err
}
defer attendeeRow.Close() // Close the row after the function returns

var newAttendee models.Attendee // Create a new Attendee struct to hold the retrieved data
if attendeeRow.Next() { // Check if there is a row to scan
err = attendeeRow.Scan(&newAttendee.AttendeeID, &newAttendee.FirstName, &newAttendee.LastName, &newAttendee.Email, &newAttendee.PhoneNumber, &newAttendee.ImageURL) // Scan the row into the newAttendee struct
if err != nil {
return nil, err
}
}

return &newAttendee, nil
}

// GetAllAttendees retrieves all attendees from the attendees table
func GetAllAttendees() ([]models.Attendee, error) {
var attendees []models.Attendee

// Query the database to fetch all attendees
rows, err := dbmap.Query(` // Query the database to fetch all attendees
SELECT attendee_id, first_name, last_name, email, phone_number, image_url
FROM attendees`)
if err != nil { // Check for errors
return nil, err
}
defer rows.Close()

for rows.Next() {
var attendee models.Attendee // Create a new Attendee struct to hold the retrieved data
err := rows.Scan(&attendee.AttendeeID, &attendee.FirstName, &attendee.LastName, &attendee.Email, &attendee.PhoneNumber, &attendee.ImageURL)
if err != nil { // Check for errors
return nil, err
}
attendees = append(attendees, attendee) // Append the attendee to the attendees
}
return attendees, nil
}
10 changes: 10 additions & 0 deletions models/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,13 @@ type Host struct {
Email string `json:"email"`
ImageURL string `json:"image_url"`
}

//attendee struct
type Attendee struct {
AttendeeID int `json:"attendee_id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
PhoneNumber string `json:"phone_number"`
ImageURL string `json:"image_url"`
}
10 changes: 10 additions & 0 deletions sql/create_attendee_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
USE events;

CREATE TABLE IF NOT EXISTS attendees (
id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
phone_number VARCHAR(20),
image_url VARCHAR(2048)
) DEFAULT CHARSET=utf8;
2 changes: 1 addition & 1 deletion sql/create_event_table.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use events;
CREATE TABLE IF NOT EXISTS `event` (
`event_id` integer PRIMARY KEY AUTO_INCREMENT,
`event_id` integer PRIMARY KEY AUTO_INCREMENT,
`title` text NOT NULL,
`date` text NOT NULL,
`time` text NOT NULL,
Expand Down