@@ -21,6 +21,7 @@ type File struct {
21
21
FavoriteCount int `json:"favorite_count" db:"-"`
22
22
IsFavoriteByMe bool `json:"is_favorite_by_me" db:"-"`
23
23
CreatedAt time.Time `json:"created_at" db:"created_at"`
24
+ UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
24
25
}
25
26
26
27
func GetFiles (ctx context.Context , userID string ) ([]* File , error ) {
@@ -164,11 +165,25 @@ func GetFileIDsInMessage(ctx context.Context, messageID string) ([]string, error
164
165
}
165
166
166
167
func InsertFiles (ctx context.Context , files []* File ) error {
168
+ uniqueFileMap := make (map [string ]* File , len (files ))
169
+ for _ , f := range files {
170
+ uniqueFileMap [f .ID ] = f
171
+ }
172
+ uniqueFiles := make ([]* File , 0 , len (uniqueFileMap ))
173
+ for _ , f := range uniqueFileMap {
174
+ uniqueFiles = append (uniqueFiles , f )
175
+ }
176
+
167
177
_ , err := db .NamedExecContext (
168
178
ctx ,
169
- `INSERT IGNORE INTO files (id, title, composer_id, composer_name, message_id, created_at)
170
- VALUES (:id, :title, :composer_id, :composer_name, :message_id, :created_at)` ,
171
- files ,
179
+ `INSERT INTO files (id, title, composer_id, composer_name, message_id, created_at)
180
+ VALUES (:id, :title, :composer_id, :composer_name, :message_id, :created_at)
181
+ ON DUPLICATE KEY UPDATE
182
+ title = VALUES(title),
183
+ composer_id = VALUES(composer_id),
184
+ composer_name = VALUES(composer_name),
185
+ message_id = VALUES(message_id)` ,
186
+ uniqueFiles ,
172
187
)
173
188
if err != nil {
174
189
return err
0 commit comments