public async Task <ActionResult <SPGetGymModel> > GetGymId([FromRoute] int id) { var resultId = await _context.Gym.FindAsync(id); if (resultId == null) { return(NotFound("Ginásio Indicado não se encontra Disponivel na BD")); } //METODO PARA SP COM PARAMETROS E RETORNO DE ERRO POR SP var ErrorCode = new ErrorHandler(); var result = new SPGetGymModel(); var SPConnection = new DatasetGeneratorFromSP(); var conn2 = new SqlConnection(); try { SqlParameter[] parameters = { new SqlParameter("@Id", id) }; conn2.ConnectionString = Configuration.GetConnectionString("MasterContext"); var DataSet = SPConnection.ConsumeSP("EXECUTE dbo.SP_GetGymId @id", parameters, conn2); var Status = JsonConvert.SerializeObject(DataSet.Tables[0]); var Validate = JsonConvert.DeserializeObject <List <Validate> >(Status); ErrorCode.CheckStatus(Validate); var ResultSp = DataSet.Tables[1].AsEnumerable(); result = ResultSp.Select(p => new SPGetGymModel { Id = p.Field <int>("GymId"), Adress = p.Field <string>("Adress"), Latitude = p.Field <string>("Latitude"), Longitude = p.Field <string>("Longitude"), Name = p.Field <string>("GymName"), Contact = p.Field <string>("Contact"), Email = p.Field <string>("Email"), Facebook = p.Field <string>("Facebook"), }).FirstOrDefault(); } catch (Exception ex) { return(BadRequest(ex.Message)); } return(result); }
public ActionResult <IEnumerable <SPGetGymModel> > GetGym() { var ErrorCode = new ErrorHandler(); var result = new List <SPGetGymModel>(); var SPConnection = new DatasetGeneratorFromSP(); var conn2 = new SqlConnection(); try { conn2.ConnectionString = Configuration.GetConnectionString("MasterContext"); var DataSet = SPConnection.ConsumeSP("EXECUTE dbo.SP_GetGym", new SqlParameter[] { }, conn2); //1o metodo para converter DataSet para/de Json var Status = JsonConvert.SerializeObject(DataSet.Tables[0]); var Validate = JsonConvert.DeserializeObject <List <Validate> >(Status); ErrorCode.CheckStatus(Validate); // Verificar os Erros que vem da BD na Tabela 0 //2o metodo para converter DataSet para/de Json var ResultSp = DataSet.Tables[1].AsEnumerable(); result = ResultSp.Select(p => new SPGetGymModel { Id = p.Field <int>("GymId"), Latitude = p.Field <string>("Latitude"), Longitude = p.Field <string>("Longitude"), Name = p.Field <string>("GymName"), Contact = p.Field <string>("Contact"), Email = p.Field <string>("Email"), Facebook = p.Field <string>("Facebook"), Adress = p.Field <string>("Adress") }).ToList(); } catch (Exception ex) { return(BadRequest(ex.Message)); } return(result); }