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

Paige Yorkman RestaurantReviews Submission #68

Open
wants to merge 2 commits 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
35 changes: 35 additions & 0 deletions RestaurantReviewsAPI/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"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/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net5.0/RestaurantReviewsAPI.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
42 changes: 42 additions & 0 deletions RestaurantReviewsAPI/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/RestaurantReviewsAPI.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/RestaurantReviewsAPI.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/RestaurantReviewsAPI.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
130 changes: 130 additions & 0 deletions RestaurantReviewsAPI/Controllers/RestaurantReviewController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using RestaurantReviewsAPI.Models;

namespace RestaurantReviewsAPI.Controllers
{
[ApiController]
[Route("[controller]")]
public class RestaurantReviewController : ControllerBase
{
private readonly ILogger<RestaurantReviewController> _logger;

public RestaurantReviewController(ILogger<RestaurantReviewController> logger)
{
_logger = logger;
}

private readonly RestaurantReviewContext _context;

public RestaurantReviewController(RestaurantReviewContext context)
{
_context = context;
}

[HttpGet]
public async Task<ActionResult<IEnumerable<RestaurantReview>>> GetRestaurantReviews()
{
return await _context.RestaurantReviewItems
.Select(x => x)
.ToListAsync();
}

[HttpGet("{id}")]
public IEnumerable<RestaurantReview> GetRestaurantReview(){
return null;
}

[HttpGet]
public async Task<ActionResult<IEnumerable<RestaurantReview>>> GetRestaurantReviewsByUser(string user)
{
return await _context.RestaurantReviewItems
.Select(x => x)
.Where(u => u.PostedByUser == user)
.ToListAsync();
}

[HttpPost]
public async Task<ActionResult<RestaurantReview>> PostRestaurantReview(RestaurantReview restaurantReview)
{

_context.RestaurantReviewItems.Add(restaurantReview);
await _context.SaveChangesAsync();

return CreatedAtAction(nameof(GetRestaurantReviews), new { id = restaurantReview.Id }, restaurantReview);
}

[HttpPost]
public async Task<ActionResult<RestaurantReview>> CreateRestaurantReview(RestaurantReview newRestaurantReview)
{
var restaurantReview = new RestaurantReview
{
DatePosted = DateTime.Now,
City = newRestaurantReview.City,
State = newRestaurantReview.State,
Review = newRestaurantReview.Review,
PostedByUser = newRestaurantReview.PostedByUser
};

_context.RestaurantReviewItems.Add(restaurantReview);
await _context.SaveChangesAsync();

return CreatedAtAction(
nameof(GetRestaurantReview),
new { id = restaurantReview.Id });
}

[HttpPut("{id}")]
public async Task<IActionResult> UpdateTodoItem(Guid id, RestaurantReview restaurantReview)
{
if (id != restaurantReview.Id)
{
return BadRequest();
}

var restaurantReviewUpdate = await _context.RestaurantReviewItems.FindAsync(id);
if (restaurantReviewUpdate == null)
{
return NotFound();
}

restaurantReviewUpdate.City = restaurantReview.City;
restaurantReviewUpdate.State = restaurantReview.State;
restaurantReviewUpdate.Review = restaurantReview.Review;

try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException) when (!RestaurantReviewExists(id))
{
return NotFound();
}

return NoContent();
}

private bool RestaurantReviewExists(Guid id) =>
_context.RestaurantReviewItems.Any(e => e.Id == id);

[HttpDelete("{id}")]
public async Task<IActionResult> DeleteRestaurantReview(long id)
{
var restaurantReview = await _context.RestaurantReviewItems.FindAsync(id);
if (restaurantReview == null)
{
return NotFound();
}

_context.RestaurantReviewItems.Remove(restaurantReview);
await _context.SaveChangesAsync();

return NoContent();
}
}
}
28 changes: 28 additions & 0 deletions RestaurantReviewsAPI/Models/RestaurantReview.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;

namespace RestaurantReviewsAPI.Models
{
public class RestaurantReview
{
public Guid Id { get; set; }

public DateTime DatePosted { get; set; }

public string City { get; set; }

public string State { get; set; }

public string Review { get; set; }

public string PostedByUser { get; set; }

[Flags]
public enum Rating {
Terrible = 1,
Poor = 2,
Acceptable = 3,
Good = 4,
Great = 5
}
}
}
14 changes: 14 additions & 0 deletions RestaurantReviewsAPI/Models/RestaurantReviewContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;

namespace RestaurantReviewsAPI.Models
{
public class RestaurantReviewContext : DbContext
{
public RestaurantReviewContext(DbContextOptions<RestaurantReviewContext> options)
: base(options)
{
}

public DbSet<RestaurantReview> RestaurantReviewItems { get; set; }
}
}
26 changes: 26 additions & 0 deletions RestaurantReviewsAPI/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace RestaurantReviewsAPI
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
31 changes: 31 additions & 0 deletions RestaurantReviewsAPI/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:26985",
"sslPort": 44370
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"RestaurantReviewsAPI": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
12 changes: 12 additions & 0 deletions RestaurantReviewsAPI/RestaurantReviewsAPI.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.11" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup>

</Project>
59 changes: 59 additions & 0 deletions RestaurantReviewsAPI/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;

namespace RestaurantReviewsAPI
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{

services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "RestaurantReviewsAPI", Version = "v1" });
});
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "RestaurantReviewsAPI v1"));
}

app.UseHttpsRedirection();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
9 changes: 9 additions & 0 deletions RestaurantReviewsAPI/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
Loading