-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateMovie.go
148 lines (135 loc) · 4.05 KB
/
updateMovie.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"context"
"database/sql"
"log"
"github.com/spf13/cobra"
"github.com/timothyrenner/movies-app/database"
)
// updateMovieCmd represents the updateMovie command
var updateMovieCmd = &cobra.Command{
Use: "update-movie",
Short: "Updates a movie from a movie page.",
Run: updateMovie,
Args: cobra.RangeArgs(1, 1),
}
func init() {
rootCmd.AddCommand(updateMovieCmd)
}
func updateMovie(cmd *cobra.Command, args []string) {
moviePageFile := args[0]
ctx := context.Background()
log.Println("Opening database.")
db, err := sql.Open("sqlite3", DB)
if err != nil {
log.Panicf("Error opening database %v:, %v", DB, err)
}
defer db.Close()
queries := database.New(db)
// Parse the page.
log.Println("Parsing the movie page.")
parser, err := CreateMovieParser()
if err != nil {
log.Panicf("Error creating parser: %v", err)
}
page, err := parser.ParsePage(moviePageFile)
if err != nil {
log.Panicf("Error parsing %v: %v", moviePageFile, err)
}
// Make a transaction here for the inserts.
log.Println("Preparing update transaction.")
insertMovieParams, err := CreateInsertMovieParams(page)
if err != nil {
log.Panicf("Error creating movie insert params: %v", err)
}
// Get the UUID from the database.
movieUuid, err := queries.FindMovie(ctx, insertMovieParams.ImdbID)
if err != nil {
log.Panicf("Error finding movie %v: %v", insertMovieParams.ImdbID, err)
}
// If there's no movie uuid use the one that was generated by
// CreateInsertMovieParams, if there is a movie uuid replace it so we
// upsert.
if movieUuid != "" {
insertMovieParams.Uuid = movieUuid
}
tx, err := db.BeginTx(ctx, nil)
if err != nil {
log.Panicf("Error creating transaction: %v", err)
}
defer tx.Rollback()
qtx := queries.WithTx(tx)
if err := qtx.InsertMovie(ctx, *insertMovieParams); err != nil {
log.Panicf("Error inserting movie: %v", err)
}
// Delete and reinsert the associated auxiliary tables we have data in the
// page for.
// Delete and repopulate actors.
if err := qtx.DeleteActorsForMovie(
ctx, movieUuid,
); err != nil {
log.Panicf("Error deleting actors for movie %v: %v", movieUuid, err)
}
insertActorsParams := CreateInsertMovieActorParams(page, movieUuid)
for ii := range insertActorsParams {
if err := qtx.InsertMovieActor(ctx, insertActorsParams[ii]); err != nil {
log.Panicf(
"Error inserting actor %v for movie: %v",
insertActorsParams[ii].Name, err,
)
}
}
// Delete and repopulate directors.
if err := qtx.DeleteDirectorsForMovie(
ctx, movieUuid,
); err != nil {
log.Panicf("Error deleting directors for movie %v: %v", movieUuid, err)
}
insertDirectorsParams := CreateInsertMovieDirectorParams(page, movieUuid)
for ii := range insertDirectorsParams {
if err := qtx.InsertMovieDirector(ctx, insertDirectorsParams[ii]); err != nil {
log.Panicf(
"Error inserting director %v for movie: %v",
insertDirectorsParams[ii].Name, err,
)
}
}
// Delete and repopulate writers.
if err := qtx.DeleteWritersForMovie(
ctx, movieUuid,
); err != nil {
log.Panicf("Error deleting writers for movie %v: %v", movieUuid, err)
}
insertWritersParams := CreateInsertMovieWriterParams(page, movieUuid)
for ii := range insertWritersParams {
if err := qtx.InsertMovieWriter(ctx, insertWritersParams[ii]); err != nil {
log.Panicf(
"Error inserting writer %v for movie: %v",
insertWritersParams[ii].Name, err,
)
}
}
// Delete and repopulate genres.
if err := qtx.DeleteGenresForMovie(
ctx, movieUuid,
); err != nil {
log.Panicf("Error deleting genres for movie %v: %v", movieUuid, err)
}
insertGenresParams := CreateInsertMovieGenreParams(page, movieUuid)
for ii := range insertGenresParams {
if err := qtx.InsertMovieGenre(ctx, insertGenresParams[ii]); err != nil {
log.Panicf(
"Error inserting genre %v for movie: %v",
insertGenresParams[ii].Name, err,
)
}
}
log.Println("Committing updates to database.")
if err := tx.Commit(); err != nil {
log.Panicf("Error committing transaction: %v", err)
}
log.Println("All done!")
}