2024-03-02 21:29:11 +01:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Weifer.Database.EF;
|
|
|
|
|
using Weifer.Database.EF.Entitys;
|
|
|
|
|
using Weifer.ShoppingApp.API.Models;
|
|
|
|
|
|
|
|
|
|
namespace Weifer.ShoppingApp.API.RestApi
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
public class ShoppingItemApiController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public readonly DatabaseContext dbContext;
|
|
|
|
|
|
|
|
|
|
public ShoppingItemApiController(DatabaseContext dbContext)
|
|
|
|
|
{
|
|
|
|
|
this.dbContext = dbContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("getItems")]
|
|
|
|
|
public async Task<IActionResult> GetMainItems([FromQuery] string customerId)
|
|
|
|
|
{
|
|
|
|
|
if (!Guid.TryParse(customerId, out var guidCustomerId))
|
|
|
|
|
{
|
|
|
|
|
return BadRequest("Invalid Customer ID");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var shoppingItems = await dbContext.ShoppingLists
|
|
|
|
|
.Where(x => x.CustomerId == guidCustomerId && x.ShoppingListName == "MainList")
|
|
|
|
|
.Include(x => x.ShoppingItems)
|
|
|
|
|
.SelectMany(x => x.ShoppingItems) // W<>hlt nur die ShoppingItems aus
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
return Ok(shoppingItems);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("addMainItem")]
|
|
|
|
|
public async Task<IActionResult> AddMainItem([FromBody] AddItemRequest request)
|
|
|
|
|
{
|
|
|
|
|
if (!Guid.TryParse(request.CustomerId, out var guidCustomerId))
|
|
|
|
|
{
|
|
|
|
|
return BadRequest("Invalid Customer ID");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var itemToAdd = new ShoppingItem
|
|
|
|
|
{
|
2024-03-03 16:16:22 +01:00
|
|
|
|
ShoppingItemId = new Guid(request.ShoppingItemId),
|
2024-03-02 21:29:11 +01:00
|
|
|
|
ShoppingListId = await dbContext.ShoppingLists
|
|
|
|
|
.Where(x => x.CustomerId == guidCustomerId && x.ShoppingListName == "MainList")
|
|
|
|
|
.Select(x => x.ShoppingListId)
|
|
|
|
|
.FirstOrDefaultAsync(),
|
|
|
|
|
Description = request.ItemName,
|
|
|
|
|
Number = 1,
|
|
|
|
|
Purchased = false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
dbContext.ShoppingItems.Add(itemToAdd);
|
|
|
|
|
await dbContext.SaveChangesAsync();
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
2024-03-03 16:16:22 +01:00
|
|
|
|
|
|
|
|
|
[HttpDelete("removeItem/{shoppingItemId}")]
|
|
|
|
|
public async Task<IActionResult> RemoveItem(string shoppingItemId)
|
|
|
|
|
{
|
|
|
|
|
if (!Guid.TryParse(shoppingItemId, out Guid guidShoppingItemId))
|
|
|
|
|
{
|
|
|
|
|
return BadRequest("Invalid GUID format for Shopping Item ID");
|
|
|
|
|
}
|
|
|
|
|
var item = dbContext.ShoppingItems.FirstOrDefault(item => item.ShoppingItemId == guidShoppingItemId);
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound("Item not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbContext.ShoppingItems.Remove(item);
|
|
|
|
|
await dbContext.SaveChangesAsync();
|
|
|
|
|
return Ok(new { message = "Item removed" });
|
|
|
|
|
}
|
2024-03-02 21:29:11 +01:00
|
|
|
|
}
|
|
|
|
|
public class AddItemRequest
|
|
|
|
|
{
|
|
|
|
|
public string CustomerId { get; set; }
|
|
|
|
|
public string ItemName { get; set; }
|
2024-03-03 16:16:22 +01:00
|
|
|
|
public string ShoppingItemId { get; set; }
|
2024-03-02 21:29:11 +01:00
|
|
|
|
}
|
|
|
|
|
}
|