···11//! `SeaORM` Entity, @generated by sea-orm-codegen 2.0
22-32#![expect(unused_imports)]
33+44pub use super::group::Entity as Group;
55pub use super::tag::Entity as Tag;
66pub use super::task::Entity as Task;
···138138 }
139139140140 // now any tags that are left inside zettel_tag_strings,
141141- // we have to put them inside the db
142142- for new_tag in tag_strings {
143143- // create a new tag
144144- let tag = TagActiveModel {
145145- name: ActiveValue::Set(new_tag),
146146- ..Default::default()
147147- }
148148- .insert(&ws.db)
149149- .await?;
141141+ // we have to look up the tags in the db and then reset them?
142142+ for tag_str in tag_strings {
143143+ // this is the tag that either already exists with this name, or we just created this new one
144144+ let tag = if let Some(existing) = TagEntity::load()
145145+ .filter_by_name(&tag_str)
146146+ .one(&ws.db)
147147+ .await?
148148+ {
149149+ existing
150150+ } else {
151151+ let am = TagActiveModel {
152152+ name: ActiveValue::Set(tag_str),
153153+ ..Default::default()
154154+ };
155155+156156+ am.insert(&ws.db).await?.into()
157157+ };
150158151159 // this zettel has this tag now
152160 let _ = ZettelTagActiveModel {
···223231224232 let (front_matter, _) = FrontMatter::extract_from_file(&ws.root.clone().join(path)).await?;
225233226226- let mut zettel_tag_strings = front_matter.tag_strings.clone();
227227-228228- zettel_tag_strings.sort();
229229-230234 // get the zettel from the db
231235 let db_zettel: ZettelModelEx = if let Some(existing_zettel) = ZettelEntity::load()
232236 .with(TagEntity)
···254258 .expect("we just inserted the zettel")
255259 };
256260257257- // get the tags for it
258258- for db_tag in db_zettel.tags {
259259- if let Ok(idx) = zettel_tag_strings.binary_search(&db_tag.name) {
260260- // we remove tags we have already processed
261261- zettel_tag_strings.remove(idx);
262262- } else {
263263- // the db says the file has tag `x`, but that tag is missing from the
264264- // front matter, we can assume its gone, lets delete that link
265265- let to_remove = ZettelTagEntity::find()
266266- .filter(ZettelTagColumns::ZettelNanoId.eq(id.0.clone()))
267267- .filter(ZettelTagColumns::TagNanoId.eq(db_tag.nano_id))
268268- .one(&ws.db)
269269- .await?
270270- .expect("this link must exist");
271271-272272- to_remove.into_active_model().delete(&ws.db).await?;
273273- }
274274- }
275275-276276- // now any tags that are left inside zettel_tag_strings,
277277- // we have to put them inside the db
278278- for new_tag in zettel_tag_strings {
279279- // create a new tag
280280- let tag = TagActiveModel {
281281- name: ActiveValue::Set(new_tag),
282282- ..Default::default()
283283- }
284284- .insert(&ws.db)
285285- .await?;
286286-287287- // this zettel has this tag now
288288- let _ = ZettelTagActiveModel {
289289- zettel_nano_id: ActiveValue::Set(id.to_string()),
290290- tag_nano_id: ActiveValue::Set(tag.nano_id.to_string()),
291291- }
292292- .insert(&ws.db)
293293- .await?;
294294- }
261261+ let mut temp_zettel: Self = db_zettel.clone().into();
262262+ temp_zettel.sync_tags(ws).await?;
295263296264 if front_matter.title != db_zettel.title {
297297- let am = ZettelActiveModel {
298298- id: ActiveValue::Unchanged(db_zettel.id),
299299- title: ActiveValue::Set(front_matter.title.clone()),
300300- ..Default::default()
301301- };
302302-265265+ let mut am = db_zettel.into_active_model();
266266+ am.title = ActiveValue::Set(front_matter.title.clone());
303267 am.update(&ws.db).await?;
304268 }
305269