aboutsummaryrefslogtreecommitdiffstats
path: root/code/api/src/Endpoints/V1/Customers/CreateCustomerRoute.cs
blob: e58aa37e6b5557ee043cc0f094484502f28ab2b0 (plain) (blame)
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
namespace IOL.GreatOffice.Api.Endpoints.V1.Customers;

public class CreateCustomerRoute : RouteBaseAsync.WithRequest<CreateCustomerPayload>.WithActionResult
{
    private readonly MainAppDatabase _database;
    private readonly ILogger<CreateCustomerRoute> _logger;
    private readonly IStringLocalizer<SharedResources> _localizer;

    public CreateCustomerRoute(MainAppDatabase database, ILogger<CreateCustomerRoute> logger, IStringLocalizer<SharedResources> localizer) {
        _database = database;
        _logger = logger;
        _localizer = localizer;
    }

    [HttpPost("~/v{version:apiVersion}/customers/create")]
    public override async Task<ActionResult> HandleAsync(CreateCustomerPayload request, CancellationToken cancellationToken = default) {
        var problem = new KnownProblemModel();
        if (request.Name.Trim().IsNullOrEmpty()) problem.AddError("name", _localizer["Name is a required field"]);
        if (problem.Errors.Any()) {
            problem.Title = _localizer["Invalid form"];
            problem.Subtitle = _localizer["One or more validation errors occured"];
            return KnownProblem(problem);
        }

        var customer = new Customer(LoggedInUser) {
            CustomerNumber = request.CustomerNumber,
            Name = request.Name,
            Description = request.Description,
            Address1 = request.Address1,
            Address2 = request.Address2,
            Country = request.Country,
            Currency = request.Currency,
            Email = request.Email,
            Phone = request.Phone,
            PostalCity = request.PostalCity,
            PostalCode = request.PostalCode,
            VATNumber = request.VATNumber,
            ORGNumber = request.ORGNumber,
            DefaultReference = request.DefaultReference,
            Website = request.Website
        };
        customer.SetOwnerIds(default, LoggedInUser.TenantId);
        _database.Customers.Add(customer);
        await _database.SaveChangesAsync(cancellationToken);
        return Ok();
    }
}

public class CreateCustomerPayload
{
    public string CustomerNumber { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string PostalCode { get; set; }
    public string PostalCity { get; set; }
    public string Country { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
    public string VATNumber { get; set; }
    public string ORGNumber { get; set; }
    public string DefaultReference { get; set; }
    public string Website { get; set; }
    public string Currency { get; set; }
}