Repository navigation
Expand file tree
/
Copy pathCustomerController.cs
More file actions
194 lines (160 loc) · 8.14 KB
/
Copy pathCustomerController.cs
File metadata and controls
194 lines (160 loc) · 8.14 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Security;
using Grand.Business.Core.Interfaces.Customers;
using Grand.Business.Core.Utilities.Customers;
using Grand.Domain.Customers;
using Grand.Domain.Permissions;
using Grand.Module.Api.Commands.Models.Customers;
using Grand.Module.Api.DTOs.Customers;
using Grand.Module.Api.Queries.Models.Customers;
using Grand.Mediator;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
namespace Grand.Module.Api.Controllers;
public class CustomerController : BaseApiController
{
private readonly ICustomerManagerService _customerManagerService;
private readonly ICustomerService _customerService;
private readonly IGroupService _groupService;
private readonly CustomerSettings _customerSettings;
private readonly IMediator _mediator;
private readonly IPermissionService _permissionService;
public CustomerController(
IMediator mediator,
IPermissionService permissionService,
ICustomerManagerService customerManagerService,
CustomerSettings customerSettings,
ICustomerService customerService,
IGroupService groupService)
{
_customerService = customerService;
_groupService = groupService;
_mediator = mediator;
_customerManagerService = customerManagerService;
_customerSettings = customerSettings;
_permissionService = permissionService;
}
[EndpointDescription("Get entity from Customer by email")]
[EndpointName("GetCustomerByEmail")]
[HttpGet]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(CustomerDto))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> Get([FromRoute] string email)
{
if (!await _permissionService.Authorize(PermissionSystemName.Customers, CurrentCustomer)) return Forbid();
var customer = await _mediator.Send(new GetCustomerQuery { Email = email });
if (customer == null) return NotFound();
return Ok(customer);
}
[EndpointDescription("Add new entity to Customer")]
[EndpointName("InsertCustomer")]
[HttpPost]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(CustomerDto))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> Post([FromBody] CustomerDto model)
{
if (!await _permissionService.Authorize(PermissionSystemName.Customers, CurrentCustomer)) return Forbid();
model = await _mediator.Send(new AddCustomerCommand { Model = model });
return Ok(model);
}
[EndpointDescription("Update entity in Customer")]
[EndpointName("UpdateCustomer")]
[HttpPut]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(CustomerDto))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> Put([FromBody] CustomerDto model)
{
if (!await _permissionService.Authorize(PermissionSystemName.Customers, CurrentCustomer)) return Forbid();
model = await _mediator.Send(new UpdateCustomerCommand { Model = model });
return Ok(model);
}
[EndpointDescription("Delete entity from Customer")]
[EndpointName("DeleteCustomer")]
[HttpDelete("{email}")]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> Delete([FromRoute] string email)
{
if (!await _permissionService.Authorize(PermissionSystemName.Customers, CurrentCustomer)) return Forbid();
var customer = await _mediator.Send(new GetCustomerQuery { Email = email });
if (customer == null) return NotFound();
await _mediator.Send(new DeleteCustomerCommand { Email = email });
return Ok();
}
//api/Customer/email/AddAddress
[EndpointDescription("Invoke action AddAddress")]
[EndpointName("AddAddress")]
[HttpPost("{email}/AddAddress")]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(AddressDto))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> AddAddress([FromRoute] string email, [FromBody] AddressDto address)
{
if (!await _permissionService.Authorize(PermissionSystemName.Customers, CurrentCustomer)) return Forbid();
var customer = await _mediator.Send(new GetCustomerQuery { Email = email });
if (customer == null) return NotFound();
address = await _mediator.Send(new AddCustomerAddressCommand { Customer = customer, Address = address });
return Ok(address);
}
//api/Customer/email/UpdateAddress
[EndpointDescription("Invoke action UpdateAddress")]
[EndpointName("UpdateAddress")]
[HttpPost("{email}/UpdateAddress")]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(AddressDto))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> UpdateAddress([FromRoute] string email, [FromBody] AddressDto address)
{
if (!await _permissionService.Authorize(PermissionSystemName.Customers, CurrentCustomer)) return Forbid();
var customer = await _mediator.Send(new GetCustomerQuery { Email = email });
if (customer == null) return NotFound();
address = await _mediator.Send(new UpdateCustomerAddressCommand { Customer = customer, Address = address });
return Ok(address);
}
//api/Customer/email/DeleteAddress
//body: { "addressId": "xxx" }
[EndpointDescription("Invoke action DeleteAddress")]
[EndpointName("DeleteAddress")]
[HttpPost("{email}/DeleteAddress")]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> DeleteAddress([FromRoute] string email, [FromBody] DeleteAddressDto model)
{
if (!await _permissionService.Authorize(PermissionSystemName.Customers, CurrentCustomer)) return Forbid();
if (model == null || string.IsNullOrEmpty(model.AddressId)) return NotFound();
var customer = await _mediator.Send(new GetCustomerQuery { Email = email });
if (customer == null) return NotFound();
var address = customer.Addresses.FirstOrDefault(x => x.Id == model.AddressId);
if (address == null) return NotFound();
await _mediator.Send(new DeleteCustomerAddressCommand { Customer = customer, Address = address });
return Ok(true);
}
//api/Customer/email/SetPassword
//body: { "password": "123456" }
[EndpointDescription("Invoke action SetPassword")]
[EndpointName("SetPassword")]
[HttpPost("{email}/SetPassword")]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> SetPassword([FromRoute] string email, [FromBody] PasswordDto model)
{
if (!await _permissionService.Authorize(PermissionSystemName.Customers, CurrentCustomer)) return Forbid();
if (model == null || string.IsNullOrEmpty(model.Password)) return NotFound();
var customer = await _customerService.GetCustomerByEmail(email);
if (customer == null) return NotFound();
//only administrators set the password of an administrator
if (await _groupService.IsAdmin(customer) && !await _groupService.IsAdmin(CurrentCustomer)) return Forbid();
await _customerManagerService.ChangePassword(customer, _customerSettings.DefaultPasswordFormat, model.Password);
return Ok(true);
}
}