func generateUniqueSlug(slug string, table string, suffix int) string { // Recursive function slugToCheck := slug if suffix > 1 { // If this is not the first try, add the suffix and try again slugToCheck = slug + "-" + strconv.Itoa(suffix) } var err error if table == "tags" { // Not needed at the moment. Tags with the same name should have the same slug. _, err = database.RetrieveTagIdBySlug(slugToCheck) } else if table == "posts" { _, err = database.RetrievePostBySlug(slugToCheck) } else if table == "users" { _, err = database.RetrieveUserBySlug(slugToCheck) } if err == nil { return generateUniqueSlug(slug, table, suffix+1) } return slugToCheck }
func UpdatePost(p *structure.Post) error { tagIds := make([]int64, 0) // Insert tags for _, tag := range p.Tags { // Tag slug might already be in database tagId, err := database.RetrieveTagIdBySlug(tag.Slug) if err != nil { // Tag is probably not in database yet tagId, err = database.InsertTag(tag.Name, tag.Slug, time.Now(), p.Author.Id) if err != nil { return err } } if tagId != 0 { tagIds = append(tagIds, tagId) } } // Update post err := database.UpdatePost(p.Id, p.Title, p.Slug, p.Markdown, p.Html, p.IsFeatured, p.IsPage, p.IsPublished, p.Image, *p.Date, p.Author.Id) if err != nil { return err } // Delete old postTags err = database.DeletePostTagsForPostId(p.Id) // Insert postTags if err != nil { return err } for _, tagId := range tagIds { err = database.InsertPostTag(p.Id, tagId) if err != nil { return err } } // Generate new global blog err = GenerateBlog() if err != nil { log.Panic("Error: couldn't generate blog data:", err) } return nil }