Skip to content
This repository was archived by the owner on Nov 13, 2021. It is now read-only.

Commit 353e3da

Browse files
pietersapMassimoC
authored andcommitted
Maturity level two issues (#104)
* copy maturity-level-one into maturity-level-two * Add empty line to fix markdown table * maturity level two * Update README.md empty guidelines - initial commit * Update CoditoRepository.cs private readonly * implement minor changes * issue fixes * issues 101, 100, 95, 94, 90 * not found problem+json in sell and delete customization * return problem+json for conflict 409
1 parent a3bc8d2 commit 353e3da

File tree

15 files changed

+193
-156
lines changed

15 files changed

+193
-156
lines changed

maturity-level-one/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
- [Versioning](#versioning)
66
- [Data Contracts](#data-contracts)
77
- [HTTP Methods](#http-methods)
8-
- [HTTP Status Codes](#http-status-codes)
8+
- [HTTP Status Codes](#http-status-codes
9+
)
910
2. [Security-first](#security-first)
1011
3. [Error Handling](#error-handling)
1112
4. [Document your APIs](#document-your-apis)

maturity-level-two/src/ContentTypeNames.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
namespace Codit.LevelTwo
1+
namespace Codit.LevelTwo
22
{
3-
public static class ContentTypeNames
3+
static class ContentTypeNames
44
{
5-
public static class Application
5+
internal static class Application
6+
67
{
78
public const string Json = "application/json";
89
public const string JsonPatch = "application/json-patch+json";

maturity-level-two/src/Controllers/v1/CustomizationController.cs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ public async Task<IActionResult> GetCustomization(int id)
7777
[SwaggerResponse((int)HttpStatusCode.BadRequest, "Invalid request")]
7878
[SwaggerResponse((int)HttpStatusCode.InternalServerError, "API is not available")]
7979
public async Task<IActionResult> Create(NewCustomizationDto newCustomization)
80-
{
81-
82-
80+
{
8381
if (newCustomization.CarId == -9999)
8482
{
8583
throw new ArgumentException("this is evil code");
@@ -117,13 +115,15 @@ public async Task<IActionResult> Create(NewCustomizationDto newCustomization)
117115
[SwaggerResponse((int)HttpStatusCode.InternalServerError, "API is not available")]
118116
public async Task<IActionResult> DeleteCustomization(int id)
119117
{
120-
var CustomizationObj = await _coditoRepository.GetCustomizationAsync(id);
121-
if (CustomizationObj == null)
118+
int numberOfChanges = await _coditoRepository.DeleteCustomizationAsync(id);
119+
if(numberOfChanges > 0)
122120
{
123-
return NotFound(new ProblemDetailsError(StatusCodes.Status404NotFound));
121+
return NoContent();
124122
}
125-
await _coditoRepository.DeleteCustomizationAsync(id);
126-
return NoContent();
123+
else
124+
{
125+
return NotFound(new ProblemDetailsError(StatusCodes.Status404NotFound));
126+
}
127127
}
128128

129129
/// <summary>
@@ -145,8 +145,9 @@ public async Task<IActionResult> UpdateIncremental(int id, [FromBody] Customizat
145145
return NotFound(new ProblemDetailsError(StatusCodes.Status404NotFound));
146146
}
147147

148+
// User passes Id as a parameter. If an Id is passed in the body, it is ignored.
149+
customizationDto.Id = id;
148150
var customizationUpdated = Mapper.Map<Customization>(customizationDto);
149-
customizationUpdated.Id = id;
150151

151152
await _coditoRepository.ApplyPatchAsync(customizationUpdated);
152153
return NoContent();
@@ -160,27 +161,21 @@ public async Task<IActionResult> UpdateIncremental(int id, [FromBody] Customizat
160161
/// <returns>Acknowledge that the c</returns>
161162
[HttpPost("{id}/sale", Name = Constants.RouteNames.v1.SellCustomization)]
162163
[SwaggerResponse((int)HttpStatusCode.Accepted, "Sale accepted. No response body")]
163-
[SwaggerResponse((int)HttpStatusCode.BadRequest, "Out of stock")]
164+
[SwaggerResponse((int)HttpStatusCode.Conflict, "Out of stock")]
164165
[SwaggerResponse((int)HttpStatusCode.NotFound, "Car customization not found")]
165166
[SwaggerResponse((int)HttpStatusCode.InternalServerError, "API is not available")]
166-
public async Task<IActionResult> VoteAsBestPlayer(int id)
167+
public async Task<IActionResult> SellCustomization(int id)
167168
{
168-
var customization = await _coditoRepository.GetCustomizationAsync(id);
169-
if (customization == null)
169+
SalesRequestResult result = await _coditoRepository.ApplyCustomizationSaleAsync(id);
170+
switch (result)
170171
{
171-
return NotFound(new ProblemDetailsError(StatusCodes.Status404NotFound));
172-
}
173-
if (customization.InventoryLevel <= 0)
174-
{
175-
return BadRequest();
172+
case SalesRequestResult.NotFound:
173+
return NotFound(new ProblemDetailsError(StatusCodes.Status404NotFound));
174+
case SalesRequestResult.OutOfStock:
175+
return Conflict(new ProblemDetailsError(StatusCodes.Status409Conflict));
176+
default:
177+
return Accepted();
176178
}
177-
178-
await _coditoRepository.ApplyCustomizationSaleAsync(customization);
179-
180-
return Accepted();
181179
}
182-
183-
184-
185180
}
186181
}

maturity-level-two/src/Controllers/v1/ErrorController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Microsoft.AspNetCore.Http;
33
using Microsoft.AspNetCore.Mvc;
44

5-
namespace Codit.LevelOne.Controllers.v1
5+
namespace Codit.LevelTwo.Controllers.v1
66
{
77
[Route("/errors")]
88
[ApiController]

maturity-level-two/src/DB/DbDataSeed.cs

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using System;
1+
using Codit.LevelTwo.Entities;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using Codit.LevelTwo.Entities;
4+
55

66
namespace Codit.LevelTwo.DB
77
{
@@ -21,52 +21,44 @@ public static void DataSeed(this CoditoContext context)
2121
Brand = "Volkswagen",
2222
Model = "Tiguan",
2323
BodyType = CarBodyType.SUV,
24-
Description = "Volkswagen's SUV model.",
25-
26-
Customizations = new List<Customization>
27-
{
28-
new Customization
29-
{
30-
NumberSold = 5,
31-
Name = "Tiguan Trendline",
32-
Url = "https://fake-url.com",
33-
InventoryLevel = 4
34-
},
35-
new Customization
36-
{
37-
NumberSold = 3,
38-
Name = "Tiguan Comfortline",
39-
Url = "https://fake-url.com",
40-
InventoryLevel = 4
41-
}
42-
}
24+
Description = "Volkswagen's SUV model."
4325
},
4426
new Car
4527
{
4628
Brand = "Skoda",
4729
Model = "Octavia Combi",
4830
BodyType = CarBodyType.Break,
49-
Description = "Skoda's most popular break.",
50-
51-
Customizations = new List<Customization>
52-
{
53-
new Customization
54-
{
55-
NumberSold = 3,
56-
Name = "Octavia Combi RS",
57-
Url = "https://fake-url.com",
58-
InventoryLevel = 8
59-
},
60-
new Customization
61-
{
62-
NumberSold = 4,
63-
Name = "Octavia Combi Scout",
64-
Url = "https://fake-url.com",
65-
InventoryLevel = 6
66-
}
67-
}
31+
Description = "Skoda's most popular break."
6832
}
6933
};
34+
cars[0].Customizations.Add(new Customization
35+
{
36+
NumberSold = 5,
37+
Name = "Tiguan Trendline",
38+
Url = "https://fake-url.com",
39+
InventoryLevel = 4
40+
});
41+
cars[0].Customizations.Add(new Customization
42+
{
43+
NumberSold = 3,
44+
Name = "Tiguan Comfortline",
45+
Url = "https://fake-url.com",
46+
InventoryLevel = 4
47+
});
48+
cars[1].Customizations.Add(new Customization
49+
{
50+
NumberSold = 3,
51+
Name = "Octavia Combi RS",
52+
Url = "https://fake-url.com",
53+
InventoryLevel = 8
54+
});
55+
cars[1].Customizations.Add(new Customization
56+
{
57+
NumberSold = 4,
58+
Name = "Octavia Combi Scout",
59+
Url = "https://fake-url.com",
60+
InventoryLevel = 6
61+
});
7062

7163
context.Cars.AddRange(cars);
7264
context.SaveChanges();

maturity-level-two/src/Entities/Car.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Codit.LevelTwo.Entities
77
public class Car
88
{
99
[Key]
10-
public int Id { get; set; }
10+
public int Id { get; private set; }
1111

1212
[Required]
1313
public string Brand { get; set; }
@@ -20,8 +20,6 @@ public class Car
2020

2121
public string Description { get; set; }
2222

23-
public ICollection<Customization> Customizations { get; set; } = new List<Customization>();
24-
25-
23+
public ICollection<Customization> Customizations { get; } = new List<Customization>();
2624
}
2725
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
using System.ComponentModel.DataAnnotations;
22
using System.ComponentModel.DataAnnotations.Schema;
33

4-
5-
6-
74
namespace Codit.LevelTwo.Entities
85
{
96
public class Customization
107
{
118
[Key]
12-
public int Id { get; set; }
9+
public int Id { get; private set; }
1310

1411
[Required]
1512
public string Name { get; set; }
@@ -19,12 +16,17 @@ public class Customization
1916

2017
public int NumberSold { get; set; }
2118

22-
2319
public int InventoryLevel { get; set; }
2420

2521
[ForeignKey(name: "CarId")]
2622
public Car Car { get; set; }
2723

2824
public int CarId { get; set; }
25+
26+
public void Sell()
27+
{
28+
NumberSold += 1;
29+
InventoryLevel -= 1;
30+
}
2931
}
3032
}

maturity-level-two/src/Open-Api-Docs.xml

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)