-
Notifications
You must be signed in to change notification settings - Fork 2
/
15. HasCodeBeenUsed.txt
33 lines (30 loc) · 1.3 KB
/
15. HasCodeBeenUsed.txt
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
public ActionResult HasCodeBeenUsed(string customerCode)
{
var token = GetToken();
if (token == null) return Unauthorized();
var customerName = "";
while (true)
{
var matches = searchService.Search(token, customerCode, "");
var match = matches.FirstOrDefault();
if (match == null)
{
if (customerName == "")
// This is the first code we have tried and it's good.
return Json("");
else
// The original code failed, but this one is ok
return Json(customerName + $" (Use {customerCode})");
}
else
{
if (customerName == "")
// The supplied code has already been used
customerName = match.CustomerName;
// Increase the number on the end of the customer code by 1 and then try that
var numericPart = int.Parse(customerCode.Substring(5));
var nextOne = numericPart+1;
customerCode = customerCode.Substring(0, 5) + nextOne.ToString("000");
}
}
}