Skip to content

Commit

Permalink
add users table
Browse files Browse the repository at this point in the history
  • Loading branch information
m1yon committed May 3, 2024
1 parent 9e070e9 commit 571bccb
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 18 deletions.
12 changes: 9 additions & 3 deletions cmd/jobsummoner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,34 @@ func main() {
mux := http.NewServeMux()
dbQueries := database.New(db)

// create our seed user
_, err = dbQueries.GetUser(ctx, 1)
if err != nil {
dbQueries.CreateUser(ctx)
}

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("cmd/jobsummoner/index.html")

if err != nil {
slog.Error("could not parse template", tint.Err(err))
}

JobPostings, err := dbQueries.GetJobPostings(ctx)
JobPostings, err := dbQueries.GetUserJobPostings(ctx, 1)

if err != nil {
slog.Error("failed to query job postings", tint.Err(err))
}

type FormattedJobPosting struct {
database.GetJobPostingsRow
database.GetUserJobPostingsRow
TimeAgo string
}

formattedJobPostings := make([]FormattedJobPosting, 0, len(JobPostings))

for _, jobPosting := range JobPostings {
formattedJobPostings = append(formattedJobPostings, FormattedJobPosting{GetJobPostingsRow: jobPosting, TimeAgo: timeAgo(jobPosting.LastPosted)})
formattedJobPostings = append(formattedJobPostings, FormattedJobPosting{GetUserJobPostingsRow: jobPosting, TimeAgo: timeAgo(jobPosting.LastPosted)})
}

homepage := struct {
Expand Down
13 changes: 13 additions & 0 deletions internal/database/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 74 additions & 0 deletions internal/database/user_job_postings.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions internal/database/users.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 30 additions & 13 deletions internal/linkedincrawler/linkedincrawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func ScrapeLoop(db *sql.DB) {
jobTypes: []string{"F"}, // F = fulltime
salaryRanges: []string{"5"}, // 5 = $120,000+
ageOfPosting: 24 * time.Hour,
userID: 1,
},
{
name: "Colorado Hybrid Roles",
Expand All @@ -42,6 +43,7 @@ func ScrapeLoop(db *sql.DB) {
jobTypes: []string{"F"},
salaryRanges: []string{"5"},
ageOfPosting: 24 * time.Hour,
userID: 1,
},
}

Expand Down Expand Up @@ -79,6 +81,7 @@ type scrapeOptions struct {
jobTypes []string
salaryRanges []string
ageOfPosting time.Duration
userID int
}

func scrape(db *sql.DB, options scrapeOptions) {
Expand Down Expand Up @@ -295,28 +298,42 @@ func scrape(db *sql.DB, options scrapeOptions) {

if err != nil {
if sqlError, ok := err.(*sqlite.Error); ok {
// fail if it's not an expected error
if sqlError.Code() != sqlite3.SQLITE_CONSTRAINT_PRIMARYKEY {
slog.Error("failed inserting job posting", slog.String("url", url.String()), tint.Err(err))
continue
}

// if the posting already exists, just update the last_posted field
if sqlError.Code() == sqlite3.SQLITE_CONSTRAINT_PRIMARYKEY {
numberOfJobRepostings++
numberOfJobRepostings++

err := dbQueries.UpdateJobPostingLastPosted(ctx, database.UpdateJobPostingLastPostedParams{
Position: positionText,
CompanyID: companySlug,
LastPosted: listingDate.UTC(),
})
err = dbQueries.UpdateJobPostingLastPosted(ctx, database.UpdateJobPostingLastPostedParams{
Position: positionText,
CompanyID: companySlug,
LastPosted: listingDate.UTC(),
})

if err != nil {
slog.Error("failed updating job posting's last_posted field", slog.String("url", url.String()), tint.Err(err))
continue
}
if err != nil {
slog.Error("failed updating job posting's last_posted field", slog.String("url", url.String()), tint.Err(err))
continue
}
}
}

err = dbQueries.CreateUserJobPosting(ctx, database.CreateUserJobPostingParams{
UserID: int64(options.userID),
Position: positionText,
CompanyID: companySlug,
})

if err != nil {
if sqlError, ok := err.(*sqlite.Error); ok {
if sqlError.Code() == sqlite3.SQLITE_CONSTRAINT_PRIMARYKEY {
continue
}
}

slog.Error("failed inserting job posting", slog.String("url", url.String()), tint.Err(err))
continue
slog.Error("failed insert user job posting", slog.String("url", url.String()), tint.Err(err))
}
}

Expand Down
7 changes: 5 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ dev-show:
go run ./cmd/jobsummoner -rod=show

reset-db:
rm -f ./database.db && make migrate-up
rm -f ./db/database.db && make migrate-up

docker-dev:
./scripts/build-docker.sh && docker compose up --build
./scripts/build-docker.sh && docker compose up --build

query-db:
sqlite3 db/database.db
21 changes: 21 additions & 0 deletions sql/migrations/003_users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- +goose Up
CREATE TABLE users(
id INTEGER PRIMARY KEY,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL
);

CREATE TABLE user_job_postings(
created_at TIMESTAMP NOT NULL,
user_id INTEGER NOT NULL,
position TEXT NOT NULL,
company_id TEXT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (position) REFERENCES job_postings(position),
FOREIGN KEY (company_id) REFERENCES job_postings(company_id)
);

-- +goose Down
DROP TABLE users;
DROP TABLE user_job_postings;

11 changes: 11 additions & 0 deletions sql/queries/user_job_postings.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- name: CreateUserJobPosting :exec
INSERT INTO user_job_postings (created_at, user_id, position, company_id)
VALUES (CURRENT_TIMESTAMP, ?, ?, ?);

-- name: GetUserJobPostings :many
SELECT job_postings.position, job_postings.url as job_posting_url, companies.name as company_name, last_posted, companies.avatar as company_avatar
FROM user_job_postings
JOIN companies on job_postings.company_id = companies.id
JOIN job_postings on user_job_postings.position = job_postings.position AND user_job_postings.company_id = job_postings.company_id
WHERE user_job_postings.user_id = ?
ORDER BY job_postings.last_posted DESC;
6 changes: 6 additions & 0 deletions sql/queries/users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- name: CreateUser :exec
INSERT INTO users (created_at, updated_at)
VALUES (CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);

-- name: GetUser :one
SELECT * FROM users WHERE id = ?;

0 comments on commit 571bccb

Please sign in to comment.