It's an API Web app working depend on a class library, you give it numbers and it will calculate the numbers and get three results depend on the parameter you will give it, Average of numbers, Smallest number and Largest number.
The Class library carry the functions, and the API value controller carry the business logic. and i make the class library as a dependency.
In the below points i will put parts from the code i use in the Class library and the API
-
Class Library
1- Average
public int Average(int[] nums) { int total = 0; foreach (var num in nums) { total += num; } return total / nums.Count(); }
2- Smallest
public int Smallest(int[] nums) { int smallest = nums[0]; foreach (var num in nums) { if (num < smallest) { smallest = num; } } return smallest; }
3- Largest
public int Largest(int[] nums) { int largest = nums[0]; foreach (var num in nums) { if (num > largest) { largest = num; } } return largest; }
Also i created a unit test for the class library to check the code covarage, please check the below unit test for Average funcation
[TestMethod]
public void TestAverrage()
{
//Arrange
Math math1 = new Math();
var nums = new int[] { 10, 33, 50, 2, 100 };
//Act
var result = math1.Average(nums);
//Assert
Assert.IsTrue(result == 39);
}
The below screenshot from the unit test result
I used Git to push my packages to the remote repo on Azure Devops
I used Azure Pipeline to run a CI on the Class Library, and store the package on Nuget.org.
You can find my Class Library package on https://www.nuget.org/packages/SherbeenyLibrary/
-
API
1- Average
[HttpGet("/average")] public int Average([FromQuery] int[] nums) { return math.Average(nums); }
2- Smallest
[HttpGet("/smallest")] public int Smallest([FromQuery] int[] nums) { return math.Smallest(nums); }
3- Largest
[HttpGet("/largest")] public int Largest([FromQuery] int[] nums) { return math.Largest(nums); }
}
Also i used Azure Pipeline to run CI/CD on the API, here is the below screenshot from the CI/CD
Continuous Integration (CI)
Note: This time i upload the package on Azure Artifact not Nuget.
Contiuous Deleviry (CD)
Note: I used MicroSoft Azure to upload My Web App but my subscription was ended.