package models

import "fmt"

// DeletePublisherByID deletes a publisher by its ID
func DeletePublisherByID(id int64) error {
	// Check if the id is 0
	if id == 0 {
		return fmt.Errorf("ID cannot be 0")
	}

	// Delete the publisher
	_, err := x.Id(id).Delete(&Publisher{})

	if err != nil {
		return err
	}

	// Set all publisher to 0 on all book with this publisher
	_, err = x.Table("books").
		Where("publisher_id = ?", id).
		Update(map[string]interface{}{"publisher_id": 0})

	return err
}
