-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Grocery Store back end to aid the grocery purchase service.
- Loading branch information
johnathan
committed
Apr 28, 2020
1 parent
81526c0
commit a2f58f8
Showing
12 changed files
with
185 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters