-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathCustomerGroupController.cs
More file actions
136 lines (113 loc) · 5.31 KB
/
CustomerGroupController.cs
File metadata and controls
136 lines (113 loc) · 5.31 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
using Grand.Business.Core.Interfaces.Common.Security;
using Grand.Domain.Customers;
using Grand.Domain.Permissions;
using Grand.Module.Api.Attributes;
using Grand.Module.Api.Commands.Models.Customers;
using Grand.Module.Api.DTOs.Customers;
using Grand.Module.Api.Queries.Models.Common;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.JsonPatch.SystemTextJson;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
namespace Grand.Module.Api.Controllers;
public class CustomerGroupController : BaseApiController
{
private readonly IMediator _mediator;
private readonly IPermissionService _permissionService;
public CustomerGroupController(IMediator mediator, IPermissionService permissionService)
{
_mediator = mediator;
_permissionService = permissionService;
}
[EndpointDescription("Get entity from CustomerGroup by key")]
[EndpointName("GetCustomerGroupById")]
[HttpGet("{key}")]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(CustomerGroupDto))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> Get([FromRoute] string key)
{
if (!await _permissionService.Authorize(PermissionSystemName.Customers)) return Forbid();
var customerGroup = await _mediator.Send(new GetGenericQuery<CustomerGroupDto, CustomerGroup>(key));
if (!customerGroup.Any()) return NotFound();
return Ok(customerGroup.FirstOrDefault());
}
[EndpointDescription("Get entities from CustomerGroup")]
[EndpointName("GetCustomerGroups")]
[HttpGet]
[EnableQuery]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<CustomerGroupDto>))]
public async Task<IActionResult> Get()
{
if (!await _permissionService.Authorize(PermissionSystemName.Customers)) return Forbid();
return Ok(await _mediator.Send(new GetGenericQuery<CustomerGroupDto, CustomerGroup>()));
}
[EndpointDescription("Add new entity to CustomerGroup")]
[EndpointName("InsertCustomerGroup")]
[HttpPost]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(CustomerGroupDto))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> Post([FromBody] CustomerGroupDto model)
{
if (!await _permissionService.Authorize(PermissionSystemName.Customers)) return Forbid();
model = await _mediator.Send(new AddCustomerGroupCommand { Model = model });
return Ok(model);
}
[EndpointDescription("Update entity in CustomerGroup")]
[EndpointName("UpdateCustomerGroup")]
[HttpPut]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(CustomerGroupDto))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> Put([FromBody] CustomerGroupDto model)
{
if (!await _permissionService.Authorize(PermissionSystemName.Customers)) return Forbid();
if (!model.IsSystem)
{
model = await _mediator.Send(new UpdateCustomerGroupCommand { Model = model });
return Ok(model);
}
return BadRequest(ModelState);
}
[EndpointDescription("Partially update entity in CustomerGroup")]
[EndpointName("PartiallyUpdateCustomerGroup")]
[HttpPatch("{key}")]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> Patch([FromRoute] string key, [FromBody] JsonPatchDocument<CustomerGroupDto> model)
{
if (string.IsNullOrEmpty(key))
return BadRequest("Key is null or empty");
if (!await _permissionService.Authorize(PermissionSystemName.Customers)) return Forbid();
var customerGroup = await _mediator.Send(new GetGenericQuery<CustomerGroupDto, CustomerGroup>(key));
if (!customerGroup.Any()) return NotFound();
var cr = customerGroup.FirstOrDefault();
model.ApplyTo(cr);
if (cr is { IsSystem: false })
{
await _mediator.Send(new UpdateCustomerGroupCommand { Model = cr });
return Ok();
}
return BadRequest(ModelState);
}
[EndpointDescription("Delete entity in CustomerGroup")]
[EndpointName("DeleteCustomerGroup")]
[HttpDelete("{key}")]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> Delete([FromRoute] string key)
{
if (!await _permissionService.Authorize(PermissionSystemName.Customers)) return Forbid();
var customerGroup = await _mediator.Send(new GetGenericQuery<CustomerGroupDto, CustomerGroup>(key));
if (!customerGroup.Any()) return NotFound();
if (customerGroup.FirstOrDefault()!.IsSystem) return Forbid();
await _mediator.Send(new DeleteCustomerGroupCommand { Model = customerGroup.FirstOrDefault() });
return Ok();
}
}