~emersion/soju

be2825595d12ee6661933c89727feaf6f3a219ce — Simon Ser 6 months ago a2c207d
Error out on network name conflict

Closes: https://todo.sr.ht/~emersion/soju/29
1 files changed, 17 insertions(+), 0 deletions(-)

M user.go
M user.go => user.go +17 -0
@@ 671,11 671,24 @@ func (u *user) removeNetwork(network *network) {
	panic("tried to remove a non-existing network")
}

func (u *user) checkNetwork(record *Network) error {
	for _, net := range u.networks {
		if net.GetName() == record.GetName() && net.ID != record.ID {
			return fmt.Errorf("a network with the name %q already exists", record.GetName())
		}
	}
	return nil
}

func (u *user) createNetwork(record *Network) (*network, error) {
	if record.ID != 0 {
		panic("tried creating an already-existing network")
	}

	if err := u.checkNetwork(record); err != nil {
		return nil, err
	}

	network := newNetwork(u, record, nil)
	err := u.srv.db.StoreNetwork(u.ID, &network.Network)
	if err != nil {


@@ 692,6 705,10 @@ func (u *user) updateNetwork(record *Network) (*network, error) {
		panic("tried updating a new network")
	}

	if err := u.checkNetwork(record); err != nil {
		return nil, err
	}

	network := u.getNetworkByID(record.ID)
	if network == nil {
		panic("tried updating a non-existing network")