Skip to content

Commit

Permalink
Added Grocery Store back end to aid the grocery purchase service.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnathan committed Apr 28, 2020
1 parent 81526c0 commit a2f58f8
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 6 deletions.
89 changes: 89 additions & 0 deletions Controllers/GroceryStoreController.cs
Original file line number Diff line number Diff line change
@@ -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<List<GroceryStoreModel>> Get() =>
_groceryStoreService.Get();

[HttpGet("{id:length(24)}", Name = "GetGroceryStore")]
public ActionResult<GroceryStoreModel> Get(string id)
{
var groceryStoreItem = _groceryStoreService.Get(id);

if (groceryStoreItem == null)
{
return NotFound();
}

return groceryStoreItem;
}

[HttpPost]
public ActionResult<GroceryStoreModel> Create(GroceryStoreModel groceryStoreModel)
{
List<GroceryStoreModel> 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();
}
}
}
5 changes: 3 additions & 2 deletions GroceriesApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.1" />
<PackageReference Include="MongoDB.Driver" Version="2.10.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.2" />
<PackageReference Include="MongoDB.Driver" Version="2.10.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.3.3" />
</ItemGroup>


Expand Down
1 change: 1 addition & 0 deletions Models/GroceryDatabaseSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Expand Down
3 changes: 2 additions & 1 deletion Models/GroceryPurchasedModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
13 changes: 13 additions & 0 deletions Models/GroceryStoreModel.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
1 change: 1 addition & 0 deletions Models/IGroceryDatabaseSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Expand Down
4 changes: 2 additions & 2 deletions Models/IGroceryModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
4 changes: 3 additions & 1 deletion Models/IGroceryPurchasedModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

}
}
13 changes: 13 additions & 0 deletions Models/IGroceryStoreModel.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
45 changes: 45 additions & 0 deletions Services/GroceryStoreService.cs
Original file line number Diff line number Diff line change
@@ -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<GroceryStoreModel> _groceryStoreModels;

public GroceryStoreService(IGroceryDatabaseSettings settings)
{
var client = new MongoClient(settings.ConnectionString);
var database = client.GetDatabase(settings.DatabaseName);

_groceryStoreModels = database.GetCollection<GroceryStoreModel>(settings.GroceryStoreCollectionName);
}

public List<GroceryStoreModel> Get() =>
_groceryStoreModels.Find(groceryStoreModel => true).ToList();

public GroceryStoreModel Get(string id) =>
_groceryStoreModels.Find<GroceryStoreModel>(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);
}
}

12 changes: 12 additions & 0 deletions Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using GroceriesApi.Models;
using Microsoft.Extensions.Options;
using GroceriesApi.Services;
using NuGet.Frameworks;

namespace GroceriesApi
{
Expand Down Expand Up @@ -42,6 +43,11 @@ public void ConfigureServices(IServiceCollection services)
services.AddSingleton<GroceryPurchasedService>();

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.
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"GroceryListCollectionName": "GroceryList",
"GroceryPurchasedCollectionName": "GroceryPurchased",
"GroceryDueDateCollectionName": "GroceryDueDate",
"GroceryStoreCollectionName": "GroceryStore",
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "Grocery"
},
Expand Down

0 comments on commit a2f58f8

Please sign in to comment.