Exemplo n.º 1
0
func UpdateCitizen(uid string, attributes Citizen) (Citizen, error) {
	var citizen Citizen

	if citizen, ok := Store.FetchCitizenByUID(uid); ok {
		if attributes.Secret != "" {
			citizen.encryptedSecret = entities.EncryptSecret([]byte(attributes.Secret))
		}

		if attributes.UID != "" {
			citizen.UID = attributes.UID
		}

		Store.SaveCitizen(citizen)
		return citizen, nil
	}

	return citizen, errors.New("Could not find citizen")
}
Exemplo n.º 2
0
func CreateCitizen(uid, secret string) (Citizen, error) {
	var citizen Citizen

	if uid == "" {
		return citizen, fmt.Errorf("UID is required")
	}

	if secret == "" {
		return citizen, fmt.Errorf("Secret is required")
	}

	if _, found := Store.FetchCitizenByUID(uid); found {
		return citizen, fmt.Errorf("UID is already taken: %v", uid)
	}

	citizen.UID = uid
	citizen.encryptedSecret = entities.EncryptSecret([]byte(secret))

	if err := Store.SaveCitizen(citizen); err != nil {
		return citizen, fmt.Errorf("There was a problem saving citizen")
	}

	return citizen, nil
}