.NET Core Web API Solution Using UiPath (RPA) - Part Three

In today’s article, we are going to create a .NET Core API project. We have seen in the first article of this series the method we will use to create it.
So to proceed we have two ways,
  • Use shortcut key “ctrl+num2” (create in first article) to open “Add New Project” window.
  • Right click on solution in VS and then select “Add New Project”.
Note
You can create your own short cut key in VS just go to Tools=>Options=>Keyboard.
 
I am using the first option, then selecting “ASP.NET Core Web Application” from list.
 
 
Then select “API” on next window
 
 
Then add the below Nuget packages, the same way we have done in first article,
  • Microsoft.EntityFrameworkCore 2.2.0
  • Microsoft.Extensions.DependencyInjection 2.2.0
Now we have to give reference of Domain and Service layer in API project. For that send hot key “ctrl+num3” (crrated in first artcile).
 
 
Then use the below activities to select the required project reference from list,
 
Type Into” activity is used to send input from keyboard, so if you see value (“[k(tab)] ”) have putted in 1st type into activity you can see a space between ] and “ , this means I am send two keyboard input :
  1. Tab (to go to checkbox list)
  2. Space (to check the checkbox)
Same way for “[k(down)] ” , where down means down arrow.
 
 
 
Add all above activities inside “Create VS Project.xaml” which was created in first article. Then add all below activities in side “Excel Sequence.xaml”.
 
As we have added dependency injection package but to implement it in our solution we can use three options,
  • Transient – Created every time when they are requested
  • Scoped – Created once per scope. Most of the time, scope refers to a web request. That means scoped services are generally created per web request.
  • Singleton – Created only for the first request and then used for whole the application life time.
We will be using scoped services pattern.
 
Take “Assign” activity and add it inside below else condition,
 
 
Then create a variable called “txtDependancyInjection” then assign below value to it,
  1. txtDependencyInjection & vbNewLine & "services.AddScoped<I"+sheet.ToString+"Service, "+sheet.ToString+"Service>();"  
So after  each activity is completed we will get  the below output in txtDependencyInjection,
  1. services.AddScoped<IAppProfileService, AppProfileService>();    
  2. services.AddScoped<IAppUserService, AppUserService>();      
  3. services.AddScoped<IRegionService, RegionService>();      
  4. services.AddScoped<ICategoryService, CategoryService>();    
Now read “startup.txt” file which contains the basic structure of startup class including required namespace reference.
 
 
Then use “Append Line” activity to create startup class file and replace
“_ DependencyInjection _” string from startup with “txtDependencyInjection”.
 
Next step is to create API controllers for each entity; for that also we have txt file the same way we had interface and services, and we will simply replace entity name into it.
 
The same process we have followed previously
 
 
After reading file store its content in a variable “txtApiContoller” and replace
  • namespace with namespace App.Api.Controllers
  • ENTITY with sheet name (sheet.ToString)
  • entity with sheet name in lower case (sheet.ToString.ToLower)
 
Store these controllers at the below location
  1. in_ProjectLocationPath+"\"+in_ProjectName+".API\Controllers  
Use “Send Hot Key“ activity to save all changes
 
 
So our App Profile controller will look like,
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Threading.Tasks;    
  5. using Microsoft.AspNetCore.Http;    
  6. using Microsoft.AspNetCore.Mvc;    
  7. using Microsoft.EntityFrameworkCore;    
  8. using App.Domain;    
  9. using App.Domain.Entities;    
  10. using App.Services.Service;    
  11. using App.Services.Interface;    
  12.     
  13. namespace App.Api.Controllers    
  14. {    
  15.     [Route("api/[controller]")]    
  16.     [ApiController]    
  17.     public class AppProfileController : ControllerBase    
  18.     {    
  19.         private readonly MyDbContext _context;    
  20.         private readonly IAppProfileService _appprofileService;    
  21.         public AppProfileController(MyDbContext context, IAppProfileService appprofileService)    
  22.         {    
  23.             _context = context;    
  24.             _appprofileService = appprofileService;    
  25.         }    
  26.     
  27.         // GET: api/AppProfile    
  28.         [HttpGet]    
  29.         public async Task<ActionResult<IEnumerable<AppProfile>>> GetAppProfile()    
  30.         {    
  31.             AppProfile appprofile = _appprofileService.GetAppProfileById(1);    
  32.             return await _context.AppProfile.ToListAsync();    
  33.         }    
  34.     
  35.         // GET: api/AppProfile/5    
  36.         [HttpGet("{id}")]    
  37.         public async Task<ActionResult<AppProfile>> GetAppProfile(int id)    
  38.         {    
  39.             var appprofile= await _context.AppProfile.FindAsync(id);    
  40.     
  41.             if (appprofile== null)    
  42.             {    
  43.                 return NotFound();    
  44.             }    
  45.     
  46.             return appprofile;    
  47.         }    
  48.     
  49.         // PUT: api/AppProfile/5    
  50.         [HttpPut("{id}")]    
  51.         public async Task<IActionResult> PutAppProfile(int id, AppProfile appprofile)    
  52.         {    
  53.             if (id != appprofile.Id)    
  54.             {    
  55.                 return BadRequest();    
  56.             }    
  57.     
  58.             _context.Entry(appprofile).State = EntityState.Modified;    
  59.     
  60.             try    
  61.             {    
  62.                 await _context.SaveChangesAsync();    
  63.             }    
  64.             catch (DbUpdateConcurrencyException)    
  65.             {    
  66.                 if (!AppProfileExists(id))    
  67.                 {    
  68.                     return NotFound();    
  69.                 }    
  70.                 else    
  71.                 {    
  72.                     throw;    
  73.                 }    
  74.             }    
  75.     
  76.             return NoContent();    
  77.         }    
  78.     
  79.         // POST: api/AppProfile    
  80.         [HttpPost]    
  81.         public async Task<ActionResult<AppProfile>> PostAppProfile(AppProfile appprofile)    
  82.         {    
  83.             _context.AppProfile.Add(appprofile);    
  84.             await _context.SaveChangesAsync();    
  85.     
  86.             return CreatedAtAction("GetAppProfile"new { id = appprofile.Id }, appprofile);    
  87.         }    
  88.     
  89.         // DELETE: api/AppProfile/5    
  90.         [HttpDelete("{id}")]    
  91.         public async Task<ActionResult<AppProfile>> DeleteAppProfilee(int id)    
  92.         {    
  93.             var appprofile = await _context.AppProfile.FindAsync(id);    
  94.             if (appprofile == null)    
  95.             {    
  96.                 return NotFound();    
  97.             }    
  98.     
  99.             _context.AppProfile.Remove(appprofile);    
  100.             await _context.SaveChangesAsync();    
  101.     
  102.             return appprofile;    
  103.         }    
  104.     
  105.         private bool AppProfileExists(int id)    
  106.         {    
  107.             return _context.AppProfile.Any(e => e.Id == id);    
  108.         }    
  109.     }    
  110. }    
Finally we get ready to use .NET Core API project and it will save us a lot of time. This is one of the fastest ways to get the base structure of a project ready.
 

Summary

 
We have used existing sequences from previous articles to continue and created API project with dependency injection.
 
In this article we have learned a lot of new things starting with UiPath Studio and its different activities, starting from excel scope to append line.
 
A small step toward automation…


Similar Articles