aboutsummaryrefslogtreecommitdiffstats
path: root/code/api/src/Endpoints/V1/Customers/CreateCustomerRoute.cs
blob: eb69f7f0443cfe94a7bd844a0fd7cdd391747ba7 (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
using Microsoft.Extensions.Localization;

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 errors = new Dictionary<string, string>();
        if (request.Name.Trim().IsNullOrEmpty()) errors.Add("name", _localizer["Name is a required field"]);
        if (errors.Any()) return KnownProblem(_localizer["Invalid form"], _localizer["One or more fields is invalid"], errors);
        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; }
}