Subscribe Us

LightBlog
Showing posts with label Cmd. Show all posts
Showing posts with label Cmd. Show all posts

Wednesday, 28 October 2020

October 28, 2020

CRUD Operations In Angular 7 Using Web API (PART-1)

 Step 1. Create a database table

Create a database. Open SQL Server and create a new database table. As you can see from the following image, I create a database table called EmployeeDetails with 7 columns.



Note: If you already have an existing database and table, you can skip this step. 
 
Step 2. Create a Web API Project
 
Now, we will create a Web API with the functionality of Create, Replace, Update, and Delete (CRUD) operations.
 
Open Visual Studio >> File >> New >> Project >> Select Web Application. After that click OK and you will see the templates. Select the Web API template.
Click OK.

To install this package, you can execute the following command from the NuGet package manager console.

Install-Package EntityFramework -Version 6.4.4
 
Step 3. Add ADO.NET Entity Data Model
 
Now, Select Models folder >> Right click >>Add >> New Item >> select Data in left panel >>ADO.NET Entity Data Model

Now click Add button then select EF Designer from database >> Next >> After that give your SQL credential and select the database where your database table and data are.
 Click the Add button and select your table and click on the Finish button.

Step 4. CRUD Operations
 
Now, we will write code to perform CRUD operation.
 
Go to the Controller folder in our API Application and right click >> Add >> Controller >> Select Web API 2 Controller-Empty 

Now, we will go to the controller class and set the routing to make it more user friendly by writing the below code.

  1. namespace CRUDAPI.Controllers  
  2. {  
  3.     [RoutePrefix("Api/Employee")]  
  4.     public class EmployeeAPIController : ApiController  
  5.     {  
  6.         WebApiDbEntities objEntity = new WebApiDbEntities();  
  7.          
  8.         [HttpGet]  
  9.         [Route("AllEmployeeDetails")]  
  10.         public IQueryable<EmployeeDetail> GetEmaployee()  
  11.         {  
  12.             try  
  13.             {  
  14.                 return objEntity.EmployeeDetails;  
  15.             }  
  16.             catch(Exception)  
  17.             {  
  18.                 throw;  
  19.             }  
  20.         }  
  21.   
  22.         [HttpGet]  
  23.         [Route("GetEmployeeDetailsById/{employeeId}")]  
  24.         public IHttpActionResult GetEmaployeeById(string employeeId)  
  25.         {  
  26.             EmployeeDetail objEmp = new EmployeeDetail();  
  27.             int ID = Convert.ToInt32(employeeId);  
  28.             try  
  29.             {  
  30.                  objEmp = objEntity.EmployeeDetails.Find(ID);  
  31.                 if (objEmp == null)  
  32.                 {  
  33.                     return NotFound();  
  34.                 }  
  35.   
  36.             }  
  37.             catch (Exception)  
  38.             {  
  39.                 throw;  
  40.             }  
  41.             
  42.             return Ok(objEmp);  
  43.         }  
  44.   
  45.         [HttpPost]  
  46.         [Route("InsertEmployeeDetails")]  
  47.         public IHttpActionResult PostEmaployee(EmployeeDetail data)  
  48.         {  
  49.              
  50.             if (!ModelState.IsValid)  
  51.             {  
  52.                 return BadRequest(ModelState);  
  53.             }  
  54.             try  
  55.             {  
  56.                 objEntity.EmployeeDetails.Add(data);  
  57.                 objEntity.SaveChanges();  
  58.             }  
  59.             catch(Exception)  
  60.             {  
  61.                 throw;  
  62.             }  
  63.   
  64.             return Ok(data);  
  65.         }  
  66.          
  67.         [HttpPut]  
  68.         [Route("UpdateEmployeeDetails")]  
  69.         public IHttpActionResult PutEmaployeeMaster(EmployeeDetail employee)  
  70.         {  
  71.             if (!ModelState.IsValid)  
  72.             {  
  73.                 return BadRequest(ModelState);  
  74.             }  
  75.   
  76.             try  
  77.             {  
  78.                 EmployeeDetail objEmp = new EmployeeDetail();  
  79.                 objEmp = objEntity.EmployeeDetails.Find(employee.EmpId);  
  80.                 if (objEmp != null)  
  81.                 {  
  82.                     objEmp.EmpName = employee.EmpName;  
  83.                     objEmp.Address = employee.Address;  
  84.                     objEmp.EmailId = employee.EmailId;  
  85.                     objEmp.DateOfBirth = employee.DateOfBirth;  
  86.                     objEmp.Gender = employee.Gender;  
  87.                     objEmp.PinCode = employee.PinCode;  
  88.   
  89.                 }  
  90.                 int i = this.objEntity.SaveChanges();  
  91.   
  92.             }  
  93.             catch(Exception)  
  94.             {  
  95.                 throw;  
  96.             }  
  97.             return Ok(employee);  
  98.         }  
  99.         [HttpDelete]  
  100.         [Route("DeleteEmployeeDetails")]  
  101.         public IHttpActionResult DeleteEmaployeeDelete(int id)  
  102.         {  
  103.             //int empId = Convert.ToInt32(id);  
  104.             EmployeeDetail emaployee = objEntity.EmployeeDetails.Find(id);  
  105.             if (emaployee == null)  
  106.             {  
  107.                 return NotFound();  
  108.             }  
  109.   
  110.             objEntity.EmployeeDetails.Remove(emaployee);  
  111.             objEntity.SaveChanges();  
  112.   
  113.             return Ok(emaployee);  
  114.         }  
  115.     }  
  116. }  
(Please refer further process for next PART-2)

Monday, 10 August 2020

August 10, 2020

Using command prompt or Git Bash publish code or files on GitLab

In this article, I am going to show, how you can push your all code into a code repository.

Create a project in GitLab

  • Goto https://gitlab.com/  Create your account follow this steps,
  • In your dashboard, click the green New project button or use the plus icon in the navigation bar. This opens the New project page
  • On the New project page, we choose  Create a blank project.

Blank projects:

  • To create a new blank project on the New project page:
  • On the Blank project tab, provide the following information:
  • The name of your project in the Project name field. You can’t use special characters, but you can use spaces, hyphens, underscores or even emoji. When adding the name, the Project slug will auto populate. The slug is what the GitLab instance will use as the URL path to the project. If you want a different slug, input the project name first, then change the slug after.
  • The path to your project in the Project slug field. This is the URL path for your project that the GitLab instance will use. If the Project name is blank, it will auto populate when you fill in the Project slug.
  • The Project description (optional) field enables you to enter a description for your project’s dashboard, which will help others understand what your project is about. Though it’s not required, it’s a good idea to fill this in.
  • Changing the Visibility Level modifies the project’s viewing and access rights for users.
  • Selecting the Initialize repository with a README option creates a README file so that the Git repository is initialized, has a default branch, and can be cloned.
  • Click Create project.

Step 1: Download git from https://git-scm.com/ and install git 

Step 2: Check git is installed on your system

  git --version

Step 3: Run following git commands

   git config --global user.name “xxxxx”

   git config --global user.name

   git config --global user.email “xxxxx”

   git config --global user.email

   git config --global --list

Step 4: Create a demo project/folder & add to git 

Step 5: Goto cmd OR terminal OR git bash 

   CD to the location of the folder and run following commands

   git init

   git status

   git add .

   git commit -m “msg” (commit your changes)

   git push -u “url” master (push your change into GitLab)

Step 6: Check project (files) added on GitLab