DEV Community

migsldev
migsldev

Posted on

Reflective Blog: My Journey Building a Real Estate Listing API

Reflective Blog: My Journey Building a Real Estate Listing API


Introduction

When I first set out to build the Real Estate Listing API, I didn't quite know what I was getting into. As an entry-level software developer, the idea of developing an API from scratch seemed intimidating. But I was eager to challenge myself and put my Python and SQL knowledge to the test. Now, looking back on this journey, I'm amazed at how much I've learned—not just about coding, but also about the importance of perseverance, the joy of problem-solving, and the thrill of seeing a project come to life.

This blog post is a reflection on my experience building this beginner Real Estate Listing API app. I'll share the highs and lows, the key learning moments, and some useful technical insights about Python and SQL that made this project both challenging and rewarding.


The Beginning: Learning the Fundamentals of Python

My journey began with the fundamentals of Python. I started by learning the basics: data types, control flow, functions, and object-oriented programming. Python's simplicity and readability made it easier for me to grasp these concepts quickly. However, the real challenge came when I had to apply these fundamentals to solve real-world problems.

Understanding object-oriented programming (OOP) was a significant milestone. I realized that by using classes and objects, I could create a structured way to handle different entities, such as Users and Properties, in my Real Estate Listing API. This laid the foundation for my project, where I needed to model real-world entities like users, properties, and applications.

For example, in my API, I defined a User model using Python classes, which helped me understand the relationship between different entities and how they interact within a system. Here's a simplified version of my User model:

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String, nullable=False)
    email = db.Column(db.String, nullable=False, unique=True)
    password = db.Column(db.String, nullable=False)
    role = db.Column(db.Enum('agent', 'property_owner', 'buyer', name='user_roles'), nullable=False)

    properties = db.relationship('Property', backref='owner', lazy=True)
    applications = db.relationship('Application', backref='applicant', lazy=True)
    favorites = db.relationship('Property', secondary='wishlist', backref='favorited_by', lazy='dynamic')
Enter fullscreen mode Exit fullscreen mode

This was my first real exposure to how Python could be used to represent real-world objects in code, and it opened up a whole new world of possibilities for me.


Diving Deeper: Building the Real Estate API

Once I had a basic understanding of Python and object-oriented programming, I decided to start building the Real Estate Listing API. The goal was simple: create an API that allows property owners to list properties, and potential renters/buyers to browse, apply, and manage their favorite properties. However, achieving this goal required a lot more than just the fundamentals.

Using Flask for RESTful API Development

Flask, a lightweight web framework for Python, became my go-to tool for building the API. I loved Flask's simplicity and flexibility; it provided just enough structure to help me get started without overwhelming me with unnecessary complexity.

I began by setting up routes to handle different HTTP methods like GET, POST, PATCH, and DELETE. This allowed me to implement the core CRUD (Create, Read, Update, Delete) operations for properties, users, applications, and wishlists. One of the things I quickly learned was the importance of returning appropriate HTTP status codes with responses. For instance, a successful POST request should return a 201 Created status, while a request for a non-existing resource should return 404 Not Found.

Here's an example of a route I created for fetching a property by its ID:

@app.route('/properties/<int:id>', methods=['GET'])
def get_property(id):
    property = Property.query.get(id)
    if property:
        return jsonify(property_schema.dump(property)), 200
    else:
        return jsonify({'error': 'Property not found'}), 404
Enter fullscreen mode Exit fullscreen mode

This snippet helped me understand how to handle different scenarios and ensure the API was providing meaningful feedback to the client.

Implementing SQLAlchemy for Database Interactions

Another crucial part of building this API was learning how to interact with the database using SQLAlchemy, an ORM (Object-Relational Mapping) tool that bridges Python classes and SQL databases. I chose SQLAlchemy because it integrates well with Flask and simplifies many of the complex aspects of SQL, like creating and managing relationships between tables.

One of the most useful technical aspects of SQLAlchemy that I used was creating many-to-many relationships. For example, in my Real Estate API, users can favorite multiple properties, and each property can be favorited by many users. To model this, I used a link table called Wishlist to manage this many-to-many relationship:

wishlist_table = db.Table('wishlist',
    db.Column('user_id', db.Integer, db.ForeignKey('user.id'), primary_key=True),
    db.Column('property_id', db.Integer, db.ForeignKey('property.id'), primary_key=True)
)
Enter fullscreen mode Exit fullscreen mode

This snippet allowed me to efficiently manage user-property relationships without creating redundant data. By using SQLAlchemy's relationship functions, I could easily query and manage these connections.

Serialization with Flask-Marshmallow

Another important learning experience was using Flask-Marshmallow to serialize my SQLAlchemy models into JSON format. Serialization converts complex data types into a format that can be easily transferred over the network, which is essential for building APIs. I used Marshmallow schemas to define how my models should be converted to JSON. Here's an example schema for my Property model:

class PropertySchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = Property
        load_instance = True

property_schema = PropertySchema()
properties_schema = PropertySchema(many=True)
Enter fullscreen mode Exit fullscreen mode

Using Marshmallow simplified the process of converting my models to JSON, allowing me to focus on building out the core functionality of the API.


Looking Back: Reflecting on the Journey

Looking back, I realize how far I've come in just a short time. When I started, I barely knew the basics of Python. Now, I've built a full-fledged API that uses Flask, SQLAlchemy, and Marshmallow, and I have a much deeper understanding of web development.

One of the most rewarding aspects of this journey was the feeling of solving problems. Every error message, every bug, and every unexpected behavior taught me something new. Whether it was figuring out why a route wasn't working, debugging a database connection issue, or learning how to properly handle CORS, each challenge helped me grow as a developer.

But perhaps the most important lesson I've learned is the value of persistence. There were times when I felt stuck or frustrated, but I kept pushing forward. I learned to break problems down into smaller, more manageable pieces and tackle them one by one.


A Useful Technical Insight: Flask CORS Configuration

One technical aspect I found particularly useful was configuring Cross-Origin Resource Sharing (CORS) in my Flask application. CORS is crucial for allowing web applications hosted on different domains to communicate with each other. In my case, it allowed the frontend (built with React) to make requests to the backend API without getting blocked by the browser's same-origin policy.

Here's how I set up CORS in my Flask app:

from flask_cors import CORS

app = Flask(__name__)
CORS(app)
Enter fullscreen mode Exit fullscreen mode

By simply adding CORS(app), I enabled cross-origin requests for all routes in my app, which made the integration between my frontend and backend much smoother. This is a small but powerful feature that every web developer should know.


Conclusion

Building the Real Estate Listing API was a challenging but immensely rewarding experience. I learned so much about Python, SQL, and web development, and I feel much more confident in my abilities as a developer. I'm excited to continue building, learning, and growing in this field, and I can't wait to see what the future holds.

For anyone just starting out, my advice is simple: keep learning, keep experimenting, and don't be afraid to make mistakes. Every challenge is an opportunity to grow, and every project is a step forward on your journey as a developer.

https://github.com/migsldev/real-estate-api

Top comments (0)