CRUD অপারেশনস (Create, Read, Update, Delete)

Database Integration এবং ORM - চেরিপাই (CherryPy) - Web Development

250

CherryPy ওয়েব ফ্রেমওয়ার্ক ব্যবহার করে আপনি খুব সহজেই CRUD (Create, Read, Update, Delete) অপারেশনস বাস্তবায়ন করতে পারেন। CRUD অপারেশনস হল ওয়েব অ্যাপ্লিকেশনের বেসিক ফিচার, যেখানে ইউজার ডেটা তৈরি, পড়া, আপডেট এবং মুছে ফেলতে পারে।

এই টিউটোরিয়ালে আমরা দেখবো কীভাবে CherryPy ব্যবহার করে একটি সাধারণ CRUD অ্যাপ তৈরি করা যায়। এখানে আমরা JSON ফরম্যাটে ডেটা সংরক্ষণ এবং পরিচালনা করবো।


1. Create (তৈরি করা)

Create অপারেশনটি নতুন ডেটা তৈরি করার জন্য ব্যবহৃত হয়। একটি নতুন রেকর্ড যুক্ত করতে HTTP POST মেথড ব্যবহার করা হয়।

উদাহরণ: Create অপারেশন

import cherrypy
import json

class MyApp:
    def __init__(self):
        self.data = []

    @cherrypy.expose
    def index(self):
        return "CRUD অ্যাপ্লিকেশনে স্বাগতম!"

    @cherrypy.expose
    def create(self, name, age):
        new_record = {"name": name, "age": age}
        self.data.append(new_record)
        return json.dumps({"status": "success", "message": "রেকর্ড তৈরি হয়েছে!"})

if __name__ == '__main__':
    cherrypy.quickstart(MyApp())

ব্যাখ্যা:

  • create মেথডে name এবং age প্যারামিটার গ্রহণ করা হয়।
  • ডেটা self.data লিস্টে সংরক্ষিত হয়।
  • json.dumps() ব্যবহার করে JSON রেসপন্স প্রদান করা হয়।

2. Read (পড়া)

Read অপারেশনটি ডেটা পড়ার জন্য ব্যবহৃত হয়। এটি সাধারণত HTTP GET মেথড ব্যবহার করে।

উদাহরণ: Read অপারেশন

import cherrypy
import json

class MyApp:
    def __init__(self):
        self.data = []

    @cherrypy.expose
    def index(self):
        return "CRUD অ্যাপ্লিকেশনে স্বাগতম!"

    @cherrypy.expose
    def create(self, name, age):
        new_record = {"name": name, "age": age}
        self.data.append(new_record)
        return json.dumps({"status": "success", "message": "রেকর্ড তৈরি হয়েছে!"})

    @cherrypy.expose
    def read(self):
        return json.dumps(self.data)

if __name__ == '__main__':
    cherrypy.quickstart(MyApp())

ব্যাখ্যা:

  • read মেথডে self.data লিস্টের সমস্ত ডেটা JSON ফরম্যাটে ফেরত দেওয়া হয়।
  • ব্রাউজারে http://127.0.0.1:8080/read URL এ গিয়ে সমস্ত ডেটা দেখতে পারবেন।

3. Update (আপডেট)

Update অপারেশনটি বিদ্যমান ডেটা আপডেট করার জন্য ব্যবহৃত হয়। এটি HTTP PUT মেথড দিয়ে কাজ করে।

উদাহরণ: Update অপারেশন

import cherrypy
import json

class MyApp:
    def __init__(self):
        self.data = []

    @cherrypy.expose
    def index(self):
        return "CRUD অ্যাপ্লিকেশনে স্বাগতম!"

    @cherrypy.expose
    def create(self, name, age):
        new_record = {"name": name, "age": age}
        self.data.append(new_record)
        return json.dumps({"status": "success", "message": "রেকর্ড তৈরি হয়েছে!"})

    @cherrypy.expose
    def read(self):
        return json.dumps(self.data)

    @cherrypy.expose
    def update(self, index, name=None, age=None):
        if index.isdigit() and int(index) < len(self.data):
            record = self.data[int(index)]
            if name:
                record["name"] = name
            if age:
                record["age"] = age
            return json.dumps({"status": "success", "message": "রেকর্ড আপডেট হয়েছে!"})
        return json.dumps({"status": "error", "message": "অবৈধ ইনডেক্স!"})

if __name__ == '__main__':
    cherrypy.quickstart(MyApp())

ব্যাখ্যা:

  • update মেথডে ইনডেক্স প্যারামিটার সহ ডেটা আপডেট করা হয়।
  • ইনডেক্সের মাধ্যমে সংশ্লিষ্ট রেকর্ডটি খুঁজে নিয়ে, name বা age প্যারামিটার গুলির মান পরিবর্তন করা হয়।
  • http://127.0.0.1:8080/update?index=0&name=John&age=30 URL দিয়ে রেকর্ড আপডেট করা যায়।

4. Delete (মুছে ফেলা)

Delete অপারেশনটি ডেটা মুছে ফেলার জন্য ব্যবহৃত হয়। এটি HTTP DELETE মেথড দিয়ে কাজ করে।

উদাহরণ: Delete অপারেশন

import cherrypy
import json

class MyApp:
    def __init__(self):
        self.data = []

    @cherrypy.expose
    def index(self):
        return "CRUD অ্যাপ্লিকেশনে স্বাগতম!"

    @cherrypy.expose
    def create(self, name, age):
        new_record = {"name": name, "age": age}
        self.data.append(new_record)
        return json.dumps({"status": "success", "message": "রেকর্ড তৈরি হয়েছে!"})

    @cherrypy.expose
    def read(self):
        return json.dumps(self.data)

    @cherrypy.expose
    def update(self, index, name=None, age=None):
        if index.isdigit() and int(index) < len(self.data):
            record = self.data[int(index)]
            if name:
                record["name"] = name
            if age:
                record["age"] = age
            return json.dumps({"status": "success", "message": "রেকর্ড আপডেট হয়েছে!"})
        return json.dumps({"status": "error", "message": "অবৈধ ইনডেক্স!"})

    @cherrypy.expose
    def delete(self, index):
        if index.isdigit() and int(index) < len(self.data):
            del self.data[int(index)]
            return json.dumps({"status": "success", "message": "রেকর্ড মুছে ফেলা হয়েছে!"})
        return json.dumps({"status": "error", "message": "অবৈধ ইনডেক্স!"})

if __name__ == '__main__':
    cherrypy.quickstart(MyApp())

ব্যাখ্যা:

  • delete মেথডে ইনডেক্স প্যারামিটার ব্যবহার করে ডেটা মুছে ফেলা হয়।
  • http://127.0.0.1:8080/delete?index=0 URL দিয়ে রেকর্ড মুছে ফেলা যায়।

সম্পূর্ণ উদাহরণ:

import cherrypy
import json

class MyApp:
    def __init__(self):
        self.data = []

    @cherrypy.expose
    def index(self):
        return "CRUD অ্যাপ্লিকেশনে স্বাগতম!"

    @cherrypy.expose
    def create(self, name, age):
        new_record = {"name": name, "age": age}
        self.data.append(new_record)
        return json.dumps({"status": "success", "message": "রেকর্ড তৈরি হয়েছে!"})

    @cherrypy.expose
    def read(self):
        return json.dumps(self.data)

    @cherrypy.expose
    def update(self, index, name=None, age=None):
        if index.isdigit() and int(index) < len(self.data):
            record = self.data[int(index)]
            if name:
                record["name"] = name
            if age:
                record["age"] = age
            return json.dumps({"status": "success", "message": "রেকর্ড আপডেট হয়েছে!"})
        return json.dumps({"status": "error", "message": "অবৈধ ইনডেক্স!"})

    @cherrypy.expose
    def delete(self, index):
        if index.isdigit() and int(index) < len(self.data):
            del self.data[int(index)]
            return json.dumps({"status": "success", "message": "রেকর্ড মুছে ফেলা হয়েছে!"})
        return json.dumps({"status": "error", "message": "অবৈধ ইনডেক্স!"})

if __name__ == '__main__':
    cherrypy.quickstart(MyApp())

CherryPy তে CRUD অপারেশনস বাস্তবায়ন করা খুবই সহজ এবং কার্যকর। আপনি HTTP POST, GET, PUT, এবং DELETE মেথডের মাধ্যমে ডেটা তৈরি, পড়া, আপডেট এবং মুছে ফেলতে পারেন। CherryPy এর এই ক্ষমতা আপনাকে একটি সম্পূর্ণ ওয়েব অ্যাপ্লিকেশন তৈরি করতে সাহায্য করবে যেখানে ডেটা পরিচালনা করা যায়।

Content added By
Promotion

Are you sure to start over?

Loading...