public Response <Producto> Add(Producto t) { // Ejecutar el validator var validator = new ProductoValidator(); var results = validator.Validate(t); if (!results.IsValid) { foreach (var failure in results.Errors) { errors.Add("Falló " + failure.PropertyName + ". Error: " + failure.ErrorMessage); } response.ErrorMessages = errors; } else { var existing = _uow.Repository.Producto.Set().Where(p => p.Codigo == t.Codigo).SingleOrDefault(); if (existing != null) { errors.Add("El código ingresado ya se encuentra registrado en el sistema"); response.ErrorMessages = errors; } else { response.item = _uow.Repository.Producto.Add(t); response.status = true; } } return(response); }
public async Task <ResultHttp> Post([FromBody] Producto x) { try { var dbparams = new DynamicParameters(); dbparams.Add("Nombre", x.Nombre, DbType.String); dbparams.Add("Precio", x.Precio, DbType.Double); dbparams.Add("Stock", x.Stock, DbType.Int32); dbparams.Add("FechaRegistro", x.FechaRegistro, DbType.DateTime); var validator = new ProductoValidator(); var result = validator.Validate(x); if (!result.IsValid) { return(new ResultHttp { Mensaje = result.Errors.FirstOrDefault().ErrorMessage, Success = false, }); } var product = await Task.FromResult(_dapper.Insert <Producto>($"InsertProduct", dbparams, commandType: CommandType.StoredProcedure)); return(new ResultHttp { Mensaje = "Se realizo correctamente lo solicitado", Success = true, Resultado = x }); } catch (Exception err) { return(new ResultHttp { Mensaje = err.Message, Success = false, }); } }