Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

my solution #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
48 changes: 48 additions & 0 deletions ClientConsole/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>

<system.serviceModel>

<client>

<!-- HTTPS-->
<!--<endpoint address="https://localhost/Reviews"
binding="webHttpBinding"
bindingConfiguration="secureBindingConfig"
contract="SoftWriters.RestaurantReviews.WebApi.IReviewApi"/>-->

<!-- HTTP-->
<endpoint address="http://localhost/Reviews2"
binding="webHttpBinding"
bindingConfiguration="httpBindingConfig"
contract="SoftWriters.RestaurantReviews.WebApi.IReviewApi"/>
</client>

<bindings>
<webHttpBinding>
<binding name="secureBindingConfig">
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>

<binding name="httpBindingConfig">
<security mode="None"/>
</binding>
</webHttpBinding>
</bindings>

<behaviors>
<endpointBehaviors>
<behavior>
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>

</system.serviceModel>

</configuration>
65 changes: 65 additions & 0 deletions ClientConsole/ClientConsole.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{CDB23559-BB93-4946-9802-9222A6586013}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>ClientConsole</RootNamespace>
<AssemblyName>ClientConsole</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReviewClient.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SoftWriters.RestaurantReviews.DataLibrary\SoftWriters.RestaurantReviews.DataLibrary.csproj">
<Project>{F2472982-DC7E-4EB8-86DD-45F1E3D189E8}</Project>
<Name>SoftWriters.RestaurantReviews.DataLibrary</Name>
</ProjectReference>
<ProjectReference Include="..\SoftWriters.RestaurantReviews.WebApi\SoftWriters.RestaurantReviews.WebApi.csproj">
<Project>{4F38EB50-BB3A-44E7-9FE2-3600FCC0BE07}</Project>
<Name>SoftWriters.RestaurantReviews.WebApi</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
178 changes: 178 additions & 0 deletions ClientConsole/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SoftWriters.RestaurantReviews.DataLibrary;

namespace ClientConsole
{
// Since unit tests test the API pretty well, we only use this console client to make sure the plumbing
// is in place and functions correctly. We don't necessarily need complete functionality here but you can
// go ahead and add functionality as needed.
// NOTE: This is good for say, a working demo before UI gets created.
class Program
{
private static string Instructions =
"\nType the appropriate number that corresponds to the api call you would like to test, then press ENTER:" +
"\n\t0\tExit" +
"\n\t1\tGet Restaurants By City" +
"\n\t2\tGet Restaurants By Zip" +
"\n\t3\tGet All Restaurants" +
"\n\t4\tAdd Restaurant" +
"\n\t5\tAdd Review" +
"\n\t6\tGet Reviews by user" +
"\n\t7\tGet Reviews by restaurant";

// We don't have any concept of logging in, so this will represent the current user
private static Guid UserId = Guid.Parse("611C77C4-DA99-4674-8252-87C9923A47D3");

static void Main(string[] args)
{
var reviewClient = new ReviewClient();
Console.WriteLine("Connected!\n");
Console.WriteLine(Instructions);

while (true)
{
var input = Console.ReadLine().Trim().ToLower();

switch (input)
{
case "0":
case "q":
reviewClient.Close();
Environment.Exit(0);
break;

case "1":
GetRestaurantsByCity(reviewClient);
break;

case "2":
GetRestaurantsByZip(reviewClient);
break;

case "3":
GetAllRestaurants(reviewClient);
break;

case "4":
AddRestaurant(reviewClient);
break;

case "5":
AddReview(reviewClient);
break;

default:
Console.WriteLine("Invalid input");
break;
}

Console.WriteLine(Instructions);
Console.Write(":");
}
}

static void GetRestaurantsByCity(ReviewClient reviewClient)
{
Console.Write("Enter city: ");
var city = Console.ReadLine();
Console.WriteLine();
var restaurants = reviewClient.GetRestaurants("", city, "", "", "");
PrintRestaurants(restaurants);
}

static void GetRestaurantsByZip(ReviewClient reviewClient)
{
Console.Write("Enter zip code: ");
var postalCode = Console.ReadLine();
Console.WriteLine();
var restaurants = reviewClient.GetRestaurants("", "", "", postalCode, "");
PrintRestaurants(restaurants);
}

static void GetAllRestaurants(ReviewClient reviewClient)
{
Console.WriteLine("Get all restaurants");
var restaurants = reviewClient.GetRestaurants("", "", "", "", "");
PrintRestaurants(restaurants);
}

static void AddRestaurant(ReviewClient reviewClient)
{
Console.Write("Enter restaurant name: ");
var restaurantName = Console.ReadLine();
Console.Write("\nEnter street address: ");
var restaurantStreet = Console.ReadLine();
Console.Write("\nEnter city: ");
var restaurantCity = Console.ReadLine();
Console.Write("\nEnter state: ");
var restaurantState = Console.ReadLine();
Console.Write("\nEnter postal code: ");
var restaurantZip = Console.ReadLine();
Console.Write("\nEnter country: ");
var restaurantCountry = Console.ReadLine();

bool result = reviewClient.AddRestaurant(restaurantName, restaurantStreet, restaurantCity, restaurantState,
restaurantZip, restaurantCountry);

Console.WriteLine("\n{0}", result ? "Add successful" : "Add failed");
}

static void AddReview(ReviewClient reviewClient)
{
Console.WriteLine("Enter the number corresponding to ");

var restaurants = reviewClient.GetRestaurants("", "", "", "", "").ToList();

TrySelectRestaurant(restaurants, out Restaurant restaurant);
Console.Write("\nEnter overall rating (1-5): ");
var overallRating = int.TryParse(Console.ReadLine(), out int oRating) ? oRating : 0;

Console.Write("\nEnter food rating (1-5): ");
var foodRating = int.TryParse(Console.ReadLine(), out int fRating) ? fRating : 0;

Console.Write("\nEnter service rating (1-5): ");
var serviceRating = int.TryParse(Console.ReadLine(), out int sRating) ? sRating : 0;

Console.Write("\nEnter cost rating (1-5): ");
var costRating = int.TryParse(Console.ReadLine(), out int cRating) ? cRating : 0;

Console.Write("Enter comments: ");
var comments = Console.ReadLine();

bool result = reviewClient.AddReview(UserId, restaurant.Id, overallRating, foodRating, serviceRating, costRating, comments);

Console.WriteLine("\n{0}", result ? "Add successful" : "Add failed");
}

static bool TrySelectRestaurant(List<Restaurant> restaurants, out Restaurant selection)
{
selection = restaurants[0];

Console.WriteLine("Select restaurant (type the number and press Enter)");
for (int i = 0; i < restaurants.Count; i++)
{
Console.WriteLine("[{0}]\t{1}", i, restaurants[i].Name);
}

var input = Console.ReadLine();
if (!int.TryParse(input, out int index))
return false;

if (index >= restaurants.Count)
return false;

selection = restaurants[index];
return true;
}

static void PrintRestaurants(IEnumerable<Restaurant> restaurants)
{
foreach (var item in restaurants)
{
Console.WriteLine(item.Name);
}
}
}
}
36 changes: 36 additions & 0 deletions ClientConsole/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ClientConsole")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ClientConsole")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("cdb23559-bb93-4946-9802-9222a6586013")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
45 changes: 45 additions & 0 deletions ClientConsole/ReviewClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;
using SoftWriters.RestaurantReviews.DataLibrary;
using SoftWriters.RestaurantReviews.WebApi;

namespace ClientConsole
{
public class ReviewClient : ClientBase<IReviewApi>, IReviewApi
{
public ReviewClient()
{ }

public ReviewClient(Binding binding, EndpointAddress remoteAddress) : base(binding, remoteAddress)
{ }

public IEnumerable<Restaurant> GetRestaurants(string street, string city, string stateCode, string postalCode, string country)
{
return Channel.GetRestaurants(street, city, stateCode, postalCode, country);
}

public IEnumerable<Review> GetReviews(Guid userId, Guid restaurantId)
{
return Channel.GetReviews(userId, restaurantId);
}

public bool AddRestaurant(string name, string street, string city, string stateCode, string postalCode, string country)
{
return Channel.AddRestaurant(name, street, city, stateCode, postalCode, country);
}

public bool AddReview(Guid userId, Guid restaurantId, int overallRating, int foodRating, int serviceRating, int costRating,
string comments)
{
return Channel.AddReview(userId, restaurantId, overallRating, foodRating, serviceRating, costRating,
comments);
}

public bool DeleteReview(Guid id)
{
return Channel.DeleteReview(id);
}
}
}
Loading