-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCartApiController.cs
128 lines (110 loc) · 3.53 KB
/
CartApiController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Sabio.Models.Domain;
using Sabio.Models.Requests.ShoppingCart;
using Sabio.Services;
using Sabio.Services.Interfaces;
using Sabio.Web.Controllers;
using Sabio.Web.Models.Responses;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Sabio.Web.Api.Controllers
{
[Route("api/cart")]
[ApiController]
public class CartApiController : BaseApiController
{
private ICartService _service = null;
private IAuthenticationService<int> _authService = null;
public CartApiController(
IAuthenticationService<int> authService
, ICartService service
, ILogger<CartApiController> logger) : base(logger)
{
_service = service;
_authService = authService;
}
[HttpPost]
public ActionResult<ItemResponse<int>> Create(CartAddRequest model)
{
int iCode = 201;
BaseResponse response = null;
try
{
int userId = _authService.GetCurrentUserId();
int id = _service.AddToCart(model.ProviderServiceId, userId);
response = new ItemResponse<int> { Item = id };
}
catch (Exception ex)
{
iCode = 500;
Logger.LogError(ex.ToString());
response = new ErrorResponse(ex.Message);
}
return StatusCode(iCode, response);
}
[HttpGet]
public ActionResult<ItemsResponse<List<ShoppingCart>>> Get()
{
int userId = _authService.GetCurrentUserId();
int iCode = 200;
BaseResponse response = null;
try
{
List<ShoppingCart> aCart = _service.GetByCreatedBy(userId);
if (aCart == null)
{
iCode = 404;
response = new ErrorResponse("Cart is Empty");
}
else
{
response = new ItemResponse<List<ShoppingCart>> { Item = aCart }; //
}
}
catch (Exception ex)
{
iCode = 500;
base.Logger.LogError(ex.ToString());
response = new ErrorResponse($"ArgumentException Error:${ex.Message}");
}
return StatusCode(iCode, response);
}
[HttpDelete("{id:int}")]
public ActionResult<SuccessResponse> DeleteItem(int id)
{
int code = 200;
BaseResponse response = null;
try
{
response = new SuccessResponse();
_service.DeleteItem(id, _authService.GetCurrentUserId());
}
catch (Exception ex)
{
code = 500;
response = new ErrorResponse(ex.Message);
}
return StatusCode(code, response);
}
[HttpDelete]
public ActionResult<SuccessResponse> DeleteCart()
{
int code = 200;
BaseResponse response = null;
try
{
response = new SuccessResponse();
_service.DeleteCart(_authService.GetCurrentUserId());
}
catch (Exception ex)
{
code = 500;
response = new ErrorResponse(ex.Message);
}
return StatusCode(code, response);
}
}
}