Ejemplo n.º 1
0
        public async Task <ActionResult <GamesResponseModel> > GetAllGames()
        {
            GamesResponseModel gamesResponseModel = new GamesResponseModel();

            gamesResponseModel = await Task.Run(() => _lessonPlannerRepository.GetAllGames());

            return(Ok(gamesResponseModel));
        }
        public GamesResponseModel GetAllGames()
        {
            GamesResponseModel gamesResponseModel = new GamesResponseModel();

            gamesResponseModel.Data = new List <GamesDto>();
            DataTable     dataTable = new DataTable();
            SqlConnection conn      = new SqlConnection(DbHelper.DbConnectionString);

            try
            {
                SqlCommand command = new SqlCommand(@"dbo.uspGetAllGames", conn);
                command.CommandType = CommandType.StoredProcedure;

                conn.Open();

                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);

                gamesResponseModel.Message    = "Success";
                gamesResponseModel.StatusCode = 200;
                foreach (DataRow row in dataTable.Rows)
                {
                    GamesDto gamesModel = new GamesDto();
                    gamesModel.GameID          = row["GameID"] != DBNull.Value ? Convert.ToInt64(row["GameID"].ToString()) : 0;
                    gamesModel.MainTopicID     = row["MainTopicID"] != DBNull.Value ? Convert.ToInt64(row["MainTopicID"].ToString()) : 0;
                    gamesModel.MainTopicNumber = row["MainTopicNumber"] != DBNull.Value ? Convert.ToString(row["MainTopicNumber"]) : string.Empty;
                    gamesModel.GameDescription = row["GameDescription"] != DBNull.Value ? Convert.ToString(row["GameDescription"].ToString()) : string.Empty;

                    gamesResponseModel.Data.Add(gamesModel);
                }
            }
            catch (Exception ex)
            {
                gamesResponseModel.StatusCode = 500;
                gamesResponseModel.Message    = ex.Message;
                gamesResponseModel.Data       = null;
            }
            finally
            {
                dataTable.Clear();
                dataTable = null;
                conn.Close();
            }

            return(gamesResponseModel);
        }