Skip to content
Closed
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
26 changes: 26 additions & 0 deletions Drinks info/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md
"name": ".drinks",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "${defaultBuildTask}",
// If you have changed target frameworks, make sure to update the program path.
"program": "C:/Users/j_men/Drinks info/Drinks/bin/Debug/net10.0/Drinks.dll",
"args": [],
"cwd": "${workspaceFolder}/Drinks",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "externalTerminal",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
41 changes: 41 additions & 0 deletions Drinks info/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"AppData/Roaming/Code/User/workspaceStorage/c68621f48698f6f8ca29549c1e8312f8/ms-dotnettools.csdevkit/Drinks info.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"AppData/Roaming/Code/User/workspaceStorage/c68621f48698f6f8ca29549c1e8312f8/ms-dotnettools.csdevkit/Drinks info.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"AppData/Roaming/Code/User/workspaceStorage/c68621f48698f6f8ca29549c1e8312f8/ms-dotnettools.csdevkit/Drinks info.sln"
],
"problemMatcher": "$msCompile"
}
]
}
16 changes: 16 additions & 0 deletions Drinks info/Drinks/Drinks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ConsoleTableExt" Version="3.3.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
<PackageReference Include="RestSharp" Version="114.0.0" />
</ItemGroup>

</Project>
125 changes: 125 additions & 0 deletions Drinks info/Drinks/DrinksService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using System.Reflection;
using System.Web;
using drinks_info.Models;
using Newtonsoft.Json;
using RestSharp;

public class DrinksService
{
// resposible for interacting with the drinks API


public List<Category> GetCategories()
{
//similar to var connection = new SqlConnection(connectionString);

var client = new RestClient("http://www.thecocktaildb.com/api/json/v1/1/");
var request = new RestRequest("List.php?c=list");
var response = client.ExecuteAsync(request);

List<Category> categories = new();

if (response.Result.StatusCode == System.Net.HttpStatusCode.OK)
{
string rawResponse = response.Result.Content;
var serialize = JsonConvert.DeserializeObject<Categories>(rawResponse);
// List<Category> returnedList = serialize.CatergoriesList;
categories = serialize.CatergoriesList;

TableVisualisationEngine.ShowTable(categories, "Categories Menu");
return categories;
}
return categories;

}



internal List<Drink> GetDrinksByCategory(string category)
{

var client = new RestClient("http://www.thecocktaildb.com/api/json/v1/1/");
var request = new RestRequest($"filter.php?c={HttpUtility.UrlEncode(category)}");
var response = client.ExecuteAsync(request);

List<Drink> drinks = new();

if (response.Result.StatusCode == System.Net.HttpStatusCode.OK)
{
string rawResponse = response.Result.Content;

var serialize = JsonConvert.DeserializeObject<Drinks>(rawResponse);
// converts the rawResponse JSON string into a Drinks object
// Drinks is the class we wrote, and serialize is the actual object that gets created from it,
// filled with the real data from the API

drinks = serialize.DrinksList;
//create a list that returns our converted rawResponse (serialize) serialize.DrinksList
// accesses a property on the object

TableVisualisationEngine.ShowTable(drinks, "Drinks List");
//print the list

return drinks;
}

return drinks;

}


internal void GetDrink(string drink)
{
var client = new RestClient("http://www.thecocktaildb.com/api/json/v1/1/");
var request = new RestRequest($"lookup.php?i={drink}");
var response = client.ExecuteAsync(request);

if (response.Result.StatusCode == System.Net.HttpStatusCode.OK)
{
string rawResponse = response.Result.Content;

var serialize = JsonConvert.DeserializeObject<DrinkDetailObject>(rawResponse);
//Takes the raw JSON string and converts it into a DrinkDetailObject

List<DrinkDetail> returnedList = serialize.DrinkDetailList;
//because DrinkDetailList lives inside DrinkDetailObject we can reach in and grab it
DrinkDetail drinkDetail = returnedList[0];
// Get the first DrinkDetail object from the list

List<object> prepList = new();
//Creates an empty list.
//The goal is to fill it with rows for the table.

string formatedName = "";

foreach (PropertyInfo prop in drinkDetail.GetType().GetProperties())
{
//drinkDetail.GetType() returns DrinkDetail
//.GetProperties() returns all properties in that class: strDrink strCategory etc
if (prop.Name.Contains("str"))

{
formatedName = prop.Name.Substring(3);
}//remove str from each property


if (!string.IsNullOrEmpty(prop.GetValue(drinkDetail)?.ToString()))
{//if drinkdetail is not empty
prepList.Add(new
{
word = formatedName,
Value = prop.GetValue(drinkDetail)
});
}
}
TableVisualisationEngine.ShowTable(prepList, "Drink Detail");

}



}



}
17 changes: 17 additions & 0 deletions Drinks info/Drinks/Models/Category.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Newtonsoft.Json;

public class Categories
{

[JsonProperty("drinks")]

public List<Category> CatergoriesList { get; set; }
}


public class Category
{
public string strCategory { get; set; }

}

65 changes: 65 additions & 0 deletions Drinks info/Drinks/Models/DrinkDetail.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace drinks_info.Models
{
internal class DrinkDetailObject
{
[JsonProperty("drinks")]
public List<DrinkDetail> DrinkDetailList { get; set; }
}

internal class DrinkDetail
{
public string strDrink { get; set; }
public object strDrinkAlternate { get; set; }
public object strTags { get; set; }
public object strVideo { get; set; }
public string strCategory { get; set; }
public object strIBA { get; set; }
public string strAlcoholic { get; set; }
public string strGlass { get; set; }
public string strInstructions { get; set; }
public object strInstructionsES { get; set; }
public string strInstructionsDE { get; set; }
public object strInstructionsFR { get; set; }
public string strInstructionsIT { get; set; }
public object strInstructionsZHHANS { get; set; }
public object strInstructionsZHHANT { get; set; }
public string strDrinkThumb { get; set; }
public string strIngredient1 { get; set; }
public string strIngredient2 { get; set; }
public string strIngredient3 { get; set; }
public string strIngredient4 { get; set; }
public object strIngredient5 { get; set; }
public object strIngredient6 { get; set; }
public object strIngredient7 { get; set; }
public object strIngredient8 { get; set; }
public object strIngredient9 { get; set; }
public object strIngredient10 { get; set; }
public object strIngredient11 { get; set; }
public object strIngredient12 { get; set; }
public object strIngredient13 { get; set; }
public object strIngredient14 { get; set; }
public object strIngredient15 { get; set; }
public string strMeasure1 { get; set; }
public string strMeasure2 { get; set; }
public string strMeasure3 { get; set; }
public string strMeasure4 { get; set; }
public object strMeasure5 { get; set; }
public object strMeasure6 { get; set; }
public object strMeasure7 { get; set; }
public object strMeasure8 { get; set; }
public object strMeasure9 { get; set; }
public object strMeasure10 { get; set; }
public object strMeasure11 { get; set; }
public object strMeasure12 { get; set; }
public object strMeasure13 { get; set; }
public object strMeasure14 { get; set; }
public object strMeasure15 { get; set; }
public object strImageSource { get; set; }
public object strImageAttribution { get; set; }
public string strCreativeCommonsConfirmed { get; set; }
public string dateModified { get; set; }
}
}
19 changes: 19 additions & 0 deletions Drinks info/Drinks/Models/Drinks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Newtonsoft.Json;

public class Drinks
{
[JsonProperty("drinks")]
//this tells the compiler when you see a JSON field called drinks, put its contents into this property

public List<Drink> DrinksList { get; set; }
//the list is the drink because its using the fields in the drink model
}

public class Drink
{

public string idDrink { get; set; }

public string strDrink { get; set; }

}
12 changes: 12 additions & 0 deletions Drinks info/Drinks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Program
{

static void Main(string[] args)
{
UserInput Input = new();
Input.GetCategoriesInput();
Console.ReadKey();

}

}
29 changes: 29 additions & 0 deletions Drinks info/Drinks/TableVisualisationEngine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Diagnostics.CodeAnalysis;
using ConsoleTableExt;

public class TableVisualisationEngine
{

// SHOWS CONSOLE DATA SPECTRE NEEDED

public static void ShowTable<Tbl>(List<Tbl> tableData, [AllowNull] string tableName) where Tbl :
class
{
Console.Clear();

if (tableName == null)
tableName = "";

Console.WriteLine("\n\n");

ConsoleTableBuilder
.From(tableData)
.WithColumn(tableName)
.ExportAndWriteLine(TableAligntment.Center);
Console.WriteLine("\n\n");


}


}
Loading