Saturday, 9 December 2023

CRUD opration using PHP API & DOT NET MVC


 PHP API :

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: *');
header('Access-Control-Allow-Headers: *');
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Max-Age: 3600");
include "conn.php";
$json = file_get_contents("php://input");
$obj= json_decode($json,true);
$mark= $obj["mark"];
if ($mark==1)
{
    $result=mysqli_query($conn,"Select * from class");
    if (mysqli_num_rows($result) > 0) {
        while($row[] = mysqli_fetch_assoc($result)) {
          $data=json_encode($row);
        }
        echo $data;
    }
    else
    {
        echo json_encode("result not found.");
    }
}
elseif($mark==2)
{
    $cID=$obj["CID"];
    $result=mysqli_query($conn,"Select * from class where CID='$cID'");
    if (mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
          $data=json_encode($row);
        }
        echo $data;
    }
    else
    {
        echo json_encode("result not found.");
    }
   
}
elseif($mark==3)
{
    $cNAME=$obj["CNAME"];
    $result=mysqli_query($conn,"INSERT INTO `class`(`CNAME`) VALUES ('$cNAME')");
    if($result)
    {
        echo json_encode("Data Inserted successfully.");
    }
    else
    {
        echo json_encode("Something went wrong.");
    }
}
elseif($mark==4)
{
    $cID=$obj["CID"];
    $CNAME=$obj["CNAME"];
    $result=mysqli_query($conn,"UPDATE `class` SET  `CNAME`='$CNAME'   WHERE CID='$cID'");
    if($result)
    {
        echo json_encode("Data Updated successfully.");
    }
    else
    {
        echo json_encode("Something went wrong.");
    }  
}
elseif($mark==5)
{
    $cID=$obj["CID"];
    $result=mysqli_query($conn,"delete from  `class` where CID='$cID'");
    if($result)
    {
        echo json_encode("Data deleted successfully.");
    }
    else
    {
        echo json_encode("Something went wrong.");
    }
}
mysqli_close($conn);
?>


DOTNET CODE :

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace WebApplication9.Models
{
    public class ClassVM
    {
        [Required(ErrorMessage ="Cid should not be blank.")]
        public int? CID { get; set; }
        [Required(ErrorMessage = "Class should not be blank.")]
        public string CNAME { get; set; }
        public string mark { get; set; }
    }
}

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Web;
using System.Web.Mvc;
using WebApplication9.Models;
using System.Threading.Tasks;
using System.Net;

namespace WebApplication9.Controllers
{
    public class ClassController : Controller
    {
        public  string baseurl;
        public ClassController()
        {
            baseurl = "https://abc.com/API/";
        }
        // GET: Class
        public ActionResult Index()
        {
                List<ClassVM> result = new List<ClassVM>();
                using (WebClient webClient = new WebClient())
                {
                ClassVM vm = new ClassVM();
                vm.mark = "1";
                 string data = JsonConvert.SerializeObject(vm);
                webClient.BaseAddress = baseurl;
                    var json =  webClient.UploadString("Class", data);
                    result = JsonConvert.DeserializeObject<List<ClassVM>>(json);
                }
                return View(result);



        }
        [HttpGet]
        public ActionResult Create()
        {
            ClassVM cvm = new ClassVM();
            return View(cvm);
        }
        [HttpPost]
        public ActionResult Create(ClassVM vm)
        {
            using (WebClient webClient = new WebClient())
            {
              
                vm.mark = "3";
                string data = JsonConvert.SerializeObject(vm);
                webClient.BaseAddress = baseurl;
                var json = webClient.UploadString("Class", data);
            }
            return RedirectToAction("Index");
           
        }
        [HttpGet]
        public ActionResult Edit(int id)
        {
            ClassVM result =new  ClassVM();
            using (WebClient webClient = new WebClient())
            {
                result.CID = id;
                result.mark = "2";
                string data = JsonConvert.SerializeObject(result);
                webClient.BaseAddress = baseurl;
                var json = webClient.UploadString("Class", data);
                result = JsonConvert.DeserializeObject<ClassVM>(json);
            }
            return View(result);

           
           
        }
        [HttpPost]
        public ActionResult Edit(ClassVM vm)
        {
            using (WebClient webClient = new WebClient())
            {

                vm.mark = "4";
                string data = JsonConvert.SerializeObject(vm);
                webClient.BaseAddress = baseurl;
                var json = webClient.UploadString("Class", data);
            }
            return RedirectToAction("Index");

        }
        [HttpGet]
        public ActionResult Delete(int id)
        {
            using (WebClient webClient = new WebClient())
            {
                ClassVM vm = new ClassVM();
                vm.CID = id;
                vm.mark = "5";
                string data = JsonConvert.SerializeObject(vm);
                webClient.BaseAddress = baseurl;
                var json = webClient.UploadString("Class", data);
            }
            return RedirectToAction("Index");
        }
    }
}



No comments:

Post a Comment