From a2f58f826155cb2e0b71eecedd0f53a4dfd9927a Mon Sep 17 00:00:00 2001 From: johnathan Date: Tue, 28 Apr 2020 18:56:02 +1200 Subject: [PATCH] Added Grocery Store back end to aid the grocery purchase service. --- Controllers/GroceryStoreController.cs | 89 +++++++++++++++++++++++++++ GroceriesApi.csproj | 5 +- Models/GroceryDatabaseSettings.cs | 1 + Models/GroceryPurchasedModel.cs | 3 +- Models/GroceryStoreModel.cs | 13 ++++ Models/IGroceryDatabaseSettings.cs | 1 + Models/IGroceryModel.cs | 4 +- Models/IGroceryPurchasedModel.cs | 4 +- Models/IGroceryStoreModel.cs | 13 ++++ Services/GroceryStoreService.cs | 45 ++++++++++++++ Startup.cs | 12 ++++ appsettings.json | 1 + 12 files changed, 185 insertions(+), 6 deletions(-) create mode 100644 Controllers/GroceryStoreController.cs create mode 100644 Models/GroceryStoreModel.cs create mode 100644 Models/IGroceryStoreModel.cs create mode 100644 Services/GroceryStoreService.cs diff --git a/Controllers/GroceryStoreController.cs b/Controllers/GroceryStoreController.cs new file mode 100644 index 0000000..af2bb1c --- /dev/null +++ b/Controllers/GroceryStoreController.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using GroceriesApi.Models; +using GroceriesApi.Services; + +namespace GroceriesApi.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class GroceryStoreController : ControllerBase + { + + private readonly GroceryStoreService _groceryStoreService; + + public GroceryStoreController(GroceryStoreService groceryStoreService) + { + _groceryStoreService = groceryStoreService; + } + + [HttpGet] + public ActionResult> Get() => + _groceryStoreService.Get(); + + [HttpGet("{id:length(24)}", Name = "GetGroceryStore")] + public ActionResult Get(string id) + { + var groceryStoreItem = _groceryStoreService.Get(id); + + if (groceryStoreItem == null) + { + return NotFound(); + } + + return groceryStoreItem; + } + + [HttpPost] + public ActionResult Create(GroceryStoreModel groceryStoreModel) + { + List currentList = _groceryStoreService.Get(); + + foreach(GroceryStoreModel listItem in currentList) + { + // checking for duplicate grocery + if (groceryStoreModel.storename.ToUpper().Trim() == listItem.storename.ToUpper().Trim()) + { + return Conflict(); + } + } + _groceryStoreService.Create(groceryStoreModel); + + return CreatedAtRoute("GetGroceryStore", new { id = groceryStoreModel.id.ToString() }, groceryStoreModel); + } + + [HttpPut("{id:length(24)}")] + public IActionResult Update(string id, GroceryStoreModel groceryStoreModelIn) + { + var groceryStoreModel = _groceryStoreService.Get(id); + + if (groceryStoreModel == null) + { + return NotFound(); + } + + _groceryStoreService.Update(id, groceryStoreModelIn); + + return NoContent(); + } + + [HttpDelete("{id:length(24)}")] + public IActionResult Delete(string id) + { + var groceryStoreModel = _groceryStoreService.Get(id); + + if (groceryStoreModel == null) + { + return NotFound(); + } + + _groceryStoreService.Remove(groceryStoreModel.id); + + return NoContent(); + } + } +} diff --git a/GroceriesApi.csproj b/GroceriesApi.csproj index 9e3a7d6..0ed5052 100644 --- a/GroceriesApi.csproj +++ b/GroceriesApi.csproj @@ -5,8 +5,9 @@ - - + + + diff --git a/Models/GroceryDatabaseSettings.cs b/Models/GroceryDatabaseSettings.cs index 0e57da1..30a7b15 100644 --- a/Models/GroceryDatabaseSettings.cs +++ b/Models/GroceryDatabaseSettings.cs @@ -10,6 +10,7 @@ public class GroceryDatabaseSettings : IGroceryDatabaseSettings public string GroceryListCollectionName { get; set; } public string GroceryPurchasedCollectionName { get; set; } public string GroceryDueDateCollectionName { get; set; } + public string GroceryStoreCollectionName { get; set; } public string ConnectionString { get; set; } public string DatabaseName { get; set; } } diff --git a/Models/GroceryPurchasedModel.cs b/Models/GroceryPurchasedModel.cs index ddbfe46..14f648d 100644 --- a/Models/GroceryPurchasedModel.cs +++ b/Models/GroceryPurchasedModel.cs @@ -15,12 +15,13 @@ public class GroceryPurchasedModel : IGroceryPurchasedModel public string id { get; set; } public decimal itemprice { get; set; } public decimal totalprice { get; set; } - public string store { get; set; } + public GroceryStoreModel store { get; set; } public string brand { get; set; } public string grocery { get; set; } public decimal quantity { get; set; } public DateTime date { get; set; } public bool onsale { get; set; } public DateTime? sellByDate { get; set; } + public string section { get; set; } } } diff --git a/Models/GroceryStoreModel.cs b/Models/GroceryStoreModel.cs new file mode 100644 index 0000000..037a1ef --- /dev/null +++ b/Models/GroceryStoreModel.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace GroceriesApi.Models +{ + public class GroceryStoreModel : IGroceryStoreModel + { + public string id { get; set; } + public string storename { get; set; } + } +} diff --git a/Models/IGroceryDatabaseSettings.cs b/Models/IGroceryDatabaseSettings.cs index ec7b99c..e697741 100644 --- a/Models/IGroceryDatabaseSettings.cs +++ b/Models/IGroceryDatabaseSettings.cs @@ -5,6 +5,7 @@ public interface IGroceryDatabaseSettings string GroceryListCollectionName { get; set; } string GroceryPurchasedCollectionName { get; set; } string GroceryDueDateCollectionName { get; set; } + string GroceryStoreCollectionName { get; set; } string ConnectionString { get; set; } string DatabaseName { get; set; } } diff --git a/Models/IGroceryModel.cs b/Models/IGroceryModel.cs index f192e57..52ce978 100644 --- a/Models/IGroceryModel.cs +++ b/Models/IGroceryModel.cs @@ -4,7 +4,7 @@ namespace GroceriesApi.Models { public interface IGroceryModel { - string id { get; set; } - string grocery { get; set; } + public string id { get; set; } + public string grocery { get; set; } } } \ No newline at end of file diff --git a/Models/IGroceryPurchasedModel.cs b/Models/IGroceryPurchasedModel.cs index d3b8f34..a74799c 100644 --- a/Models/IGroceryPurchasedModel.cs +++ b/Models/IGroceryPurchasedModel.cs @@ -8,8 +8,10 @@ public interface IGroceryPurchasedModel : IGroceryModel, IGroceryDueDate { decimal itemprice { get; set; } decimal totalprice { get; set; } - string store { get; set; } + GroceryStoreModel store { get; set; } DateTime date { get; set; } bool onsale { get; set; } + string section { get; set; } + } } diff --git a/Models/IGroceryStoreModel.cs b/Models/IGroceryStoreModel.cs new file mode 100644 index 0000000..02da38d --- /dev/null +++ b/Models/IGroceryStoreModel.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace GroceriesApi.Models +{ + interface IGroceryStoreModel + { + string id { get; set; } + string storename { get; set; } + } +} diff --git a/Services/GroceryStoreService.cs b/Services/GroceryStoreService.cs new file mode 100644 index 0000000..2374f8c --- /dev/null +++ b/Services/GroceryStoreService.cs @@ -0,0 +1,45 @@ +using System; +using MongoDB.Driver; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using GroceriesApi.Models; + +namespace GroceriesApi.Services +{ + public class GroceryStoreService + { + + private readonly IMongoCollection _groceryStoreModels; + + public GroceryStoreService(IGroceryDatabaseSettings settings) + { + var client = new MongoClient(settings.ConnectionString); + var database = client.GetDatabase(settings.DatabaseName); + + _groceryStoreModels = database.GetCollection(settings.GroceryStoreCollectionName); + } + + public List Get() => + _groceryStoreModels.Find(groceryStoreModel => true).ToList(); + + public GroceryStoreModel Get(string id) => + _groceryStoreModels.Find(groceryStoreModel => groceryStoreModel.id == id).FirstOrDefault(); + + public GroceryStoreModel Create(GroceryStoreModel groceryStoreModel) + { + _groceryStoreModels.InsertOne(groceryStoreModel); + return groceryStoreModel; + } + + public void Update(string id, GroceryStoreModel groceryStoreModelIn) => + _groceryStoreModels.ReplaceOne(groceryStoreModel => groceryStoreModel.id == id, groceryStoreModelIn); + + public void Remove(GroceryStoreModel groceryStoreModelIn) => + _groceryStoreModels.DeleteOne(groceryStoreModel => groceryStoreModel.id == groceryStoreModelIn.id); + + public void Remove(string id) => + _groceryStoreModels.DeleteOne(groceryStoreModel => groceryStoreModel.id == id); + } +} + diff --git a/Startup.cs b/Startup.cs index cc87fc6..58854f8 100644 --- a/Startup.cs +++ b/Startup.cs @@ -13,6 +13,7 @@ using GroceriesApi.Models; using Microsoft.Extensions.Options; using GroceriesApi.Services; +using NuGet.Frameworks; namespace GroceriesApi { @@ -42,6 +43,11 @@ public void ConfigureServices(IServiceCollection services) services.AddSingleton(); services.AddControllers(); + + services.AddSwaggerGen( gen => + { + gen.SwaggerDoc("v1.0", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "Grocery Manager API", Version = "V1.0" }); + }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -58,6 +64,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseAuthorization(); + app.UseSwagger(); + + app.UseSwaggerUI(ui => { + ui.SwaggerEndpoint("/swagger/v1.0/swagger.json", "Grocery Manager API Endpoint"); + }); + app.UseEndpoints(endpoints => { endpoints.MapDefaultControllerRoute(); diff --git a/appsettings.json b/appsettings.json index b745a44..eafbd37 100644 --- a/appsettings.json +++ b/appsettings.json @@ -3,6 +3,7 @@ "GroceryListCollectionName": "GroceryList", "GroceryPurchasedCollectionName": "GroceryPurchased", "GroceryDueDateCollectionName": "GroceryDueDate", + "GroceryStoreCollectionName": "GroceryStore", "ConnectionString": "mongodb://localhost:27017", "DatabaseName": "Grocery" },