Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Barberia/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/*
.trace
.coverage*
36 changes: 36 additions & 0 deletions Barberia/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[package]
name = "Barberia"
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move
# license = "" # e.g., "MIT", "GPL", "Apache 2.0"
# authors = ["..."] # e.g., ["Joe Smith (joesmith@noemail.com)", "John Snow (johnsnow@noemail.com)"]

[dependencies]

# For remote import, use the `{ git = "...", subdir = "...", rev = "..." }`.
# Revision can be a branch, a tag, and a commit hash.
# MyRemotePackage = { git = "https://some.remote/host.git", subdir = "remote/path", rev = "main" }

# For local dependencies use `local = path`. Path is relative to the package root
# Local = { local = "../path/to" }

# To resolve a version conflict and force a specific version for dependency
# override use `override = true`
# Override = { local = "../conflicting/version", override = true }

[addresses]
barberia = "0x0"

# Named addresses will be accessible in Move as `@name`. They're also exported:
# for example, `std = "0x1"` is exported by the Standard Library.
# alice = "0xA11CE"

[dev-dependencies]
# The dev-dependencies section allows overriding dependencies for `--test` and
# `--dev` modes. You can introduce test-only dependencies here.
# Local = { local = "../path/to/dev-build" }

[dev-addresses]
# The dev-addresses section allows overwriting named addresses for the `--test`
# and `--dev` modes.
# alice = "0xB0B"

113 changes: 113 additions & 0 deletions Barberia/sources/barberia.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
module barberia::registro_barberia {
use std::string::{String, utf8};
use sui::vec_map::{Self, VecMap};


// Objeto principal Barberia que almacena nombre y un VecMap del dueño ID al Cliente
public struct Barberia has key, store {
id: UID,
nombre_barberia: String,
clientes: VecMap<u64, Cliente>,
}

// Cliente con nombre y vector de servicios tomados
public struct Cliente has copy, drop, store {
nombre_cliente: String,
servicios_tomados: vector<ServicioTomado>,
}

// Enum para servicios tomados: Corte, Afeitado, Delineado.
public enum ServicioTomado has store, drop, copy {
Corte(Corte),
Afeitado(Afeitado),
Delineado(Delineado),
}

// Estructura Corte con duración en minutos y precio
public struct Corte has copy, drop, store {
duracion_minutos: u64,
precio: u64,
}

// Estructura Delineado con tipo de afeitado y precio
public struct Afeitado has copy, drop, store {
tipo: String,
precio: u64,
}

// Estructura Afeitado con tipo de afeitado y precio
public struct Delineado has copy, drop, store {
tipo: String,
precio: u64,
}

#[error]
const ID_YA_EXISTE: vector<u8> = b"ERROR el id ya existe";
#[error]
const ID_NO_EXISTE: vector<u8> = b"ERROR el id no existe";


public fun crear_barberia(nombre: String, ctx: &mut TxContext) {
let barberia = Barberia {
id: object::new(ctx),
nombre_barberia: nombre,
clientes: vec_map::empty(),
};
transfer::transfer(barberia, tx_context::sender(ctx));
}

public fun agregar_cliente(barberia: &mut Barberia, id_cliente: u64, nombre_cliente: String) {
assert!(!barberia.clientes.contains(&id_cliente), ID_YA_EXISTE);
let cliente = Cliente {
nombre_cliente,
servicios_tomados: vector[],
};
barberia.clientes.insert(id_cliente, cliente);
}

public fun agregar_corte(barberia: &mut Barberia, id_cliente: u64, duracion_minutos: u64, precio: u64) {
assert!(barberia.clientes.contains(&id_cliente), ID_NO_EXISTE);

let nuevo_servicio = ServicioTomado::Corte(Corte { duracion_minutos, precio });

let cliente_ref = barberia.clientes.get_mut(&id_cliente);
cliente_ref.servicios_tomados.push_back(nuevo_servicio);
}

public fun agregar_afeitado(barberia: &mut Barberia, id_cliente: u64, tipo: String, precio: u64) {
assert!(barberia.clientes.contains(&id_cliente), ID_NO_EXISTE);

let nuevo_servicio = ServicioTomado::Afeitado(Afeitado { tipo, precio });

let cliente_ref = barberia.clientes.get_mut(&id_cliente);
cliente_ref.servicios_tomados.push_back(nuevo_servicio);
}

public fun modificar_servicio(barberia: &mut Barberia, id_cliente: u64, indice_servicio: u64, nuevo_nombre: String, nuevo_precio: u64) {

//Verificar que el cliente exista
assert!(barberia.clientes.contains(&id_cliente), ID_NO_EXISTE);

//Obtener referencia mutable al cliente
let cliente_ref = barberia.clientes.get_mut(&id_cliente);

//Verificar que el índice del servicio sea válido
assert!(indice_servicio < vector::length(&cliente_ref.servicios_tomados), ID_NO_EXISTE);

let servicio_ref = cliente_ref.servicios_tomados.borrow_mut(indice_servicio);

match (servicio_ref) {
ServicioTomado::Corte(corte) => {
corte.precio = nuevo_precio;
},
ServicioTomado::Afeitado(afeitado) => {
afeitado.tipo = nuevo_nombre;
afeitado.precio = nuevo_precio;
},
ServicioTomado::Delineado(delineado) => {
delineado.tipo = nuevo_nombre;
delineado.precio = nuevo_precio;
}
}
}
}
18 changes: 18 additions & 0 deletions Barberia/tests/barberia_tests.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
#[test_only]
module barberia::barberia_tests;
// uncomment this line to import the module
// use barberia::barberia;

const ENotImplemented: u64 = 0;

#[test]
fun test_barberia() {
// pass
}

#[test, expected_failure(abort_code = ::barberia::barberia_tests::ENotImplemented)]
fun test_barberia_fail() {
abort ENotImplemented
}
*/