Fakultas Ilmu Komputer UI

Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.

BambangShop Publisher App

Tutorial and Example for Advanced Programming 2024 - Faculty of Computer Science, Universitas Indonesia


About this Project

In this repository, we have provided you a REST (REpresentational State Transfer) API project using Rocket web framework.

This project consists of four modules:

  1. controller: this module contains handler functions used to receive request and send responses. In Model-View-Controller (MVC) pattern, this is the Controller part.
  2. model: this module contains structs that serve as data containers. In MVC pattern, this is the Model part.
  3. service: this module contains structs with business logic methods. In MVC pattern, this is also the Model part.
  4. repository: this module contains structs that serve as databases and methods to access the databases. You can use methods of the struct to get list of objects, or operating an object (create, read, update, delete).

This repository provides a basic functionality that makes BambangShop work: ability to create, read, and delete Products. This repository already contains a functioning Product model, repository, service, and controllers that you can try right away.

As this is an Observer Design Pattern tutorial repository, you need to implement another feature: Notification. This feature will notify creation, promotion, and deletion of a product, to external subscribers that are interested of a certain product type. The subscribers are another Rocket instances, so the notification will be sent using HTTP POST request to each subscriber's receive notification address.

API Documentations

You can download the Postman Collection JSON here: https://ristek.link/AdvProgWeek7Postman

After you download the Postman Collection, you can try the endpoints inside "BambangShop Publisher" folder. This Postman collection also contains endpoints that you need to implement later on (the Notification feature).

Postman is an installable client that you can use to test web endpoints using HTTP request. You can also make automated functional testing scripts for REST API projects using this client. You can install Postman via this website: https://www.postman.com/downloads/

How to Run in Development Environment

  1. Set up environment variables first by creating .env file. Here is the example of .env file:
    APP_INSTANCE_ROOT_URL="http://localhost:8000"
    Here are the details of each environment variable:
    variable type description
    APP_INSTANCE_ROOT_URL string URL address where this publisher instance can be accessed.
  2. Use cargo run to run this app. (You might want to use cargo check if you only need to verify your work without running the app.)

Mandatory Checklists (Publisher)

  • [✓] Clone https://gitlab.com/ichlaffterlalu/bambangshop to a new repository.
  • STAGE 1: Implement models and repositories
    • [✓] Commit: Create Subscriber model struct.
    • [✓] Commit: Create Notification model struct.
    • [✓] Commit: Create Subscriber database and Subscriber repository struct skeleton.
    • [✓] Commit: Implement add function in Subscriber repository.
    • [✓] Commit: Implement list_all function in Subscriber repository.
    • [✓] Commit: Implement delete function in Subscriber repository.
    • [✓] Write answers of your learning module's "Reflection Publisher-1" questions in this README.
  • STAGE 2: Implement services and controllers
    • [✓] Commit: Create Notification service struct skeleton.
    • [✓] Commit: Implement subscribe function in Notification service.
    • [✓] Commit: Implement subscribe function in Notification controller.
    • [✓] Commit: Implement unsubscribe function in Notification service.
    • [✓] Commit: Implement unsubscribe function in Notification controller.
    • [✓] Write answers of your learning module's "Reflection Publisher-2" questions in this README.
  • STAGE 3: Implement notification mechanism
    • Commit: Implement update method in Subscriber model to send notification HTTP requests.
    • Commit: Implement notify function in Notification service to notify each Subscriber.
    • Commit: Implement publish function in Program service and Program controller.
    • Commit: Edit Product service methods to call notify after create/delete.
    • Write answers of your learning module's "Reflection Publisher-3" questions in this README.

Your Reflections

This is the place for you to write reflections:

Mandatory (Publisher) Reflections

Reflection Publisher-1

  1. In the Observer pattern diagram explained by the Head First Design Pattern book, Subscriber is defined as an interface. Explain based on your understanding of Observer design patterns, do we still need an interface (or trait in Rust) in this BambangShop case, or a single Model struct is enough?

Berdasarkan pemahaman terhadap Observer pattern pada BambangShop saat ini, penggunaan single Model struct sudah cukup. Hal ini karena:

- Saat ini hanya ada satu tipe subscriber (Observer), sehingga belum ada kebutuhan untuk abstraksi atau polimorfisme melalui trait atau interface.

- Jika di masa depan muncul tipe-tipe subscriber baru dengan perilaku yang berbeda, penggunaan trait atau interface akan diperlukan untuk memisahkan perilaku observer dari implementasinya dan untuk menerapkan prinsip Open/Closed, di mana publisher tidak perlu diubah ketika menambah jenis subscriber baru.

Dengan demikian, untuk saat ini solusi yang sederhana sudah memadai, dan kita bisa mempertimbangkan refactoring ke penggunaan trait apabila kompleksitas sistem bertambah. Pendekatan ini sesuai dengan prinsip desain SOLID, khususnya Open/Closed Principle, sehingga sistem dapat berkembang tanpa mengganggu komponen yang telah ada.



  1. id in Program and url in Subscriber is intended to be unique. Explain based on your understanding, is using Vec (list) sufficient or using DashMap (map/dictionary) like we currently use is necessary for this case?

    Mempertimbangkan keunggulan dalam pencarian dan manajemen data, DashMap menawarkan solusi yang lebih optimal. Pertama, DashMap menyediakan operasi lookup berdasarkan id atau url dengan kompleksitas rata-rata O(1), berbeda dengan Vec yang dapat mencapai kompleksitas O(n) dalam pencarian. Selain itu, DashMap memungkinkan penyimpanan semua data terkait dalam satu struktur, sehingga kita tidak perlu mengelola dua struktur yang berbeda seperti yang mungkin terjadi dengan Vec. Fitur dukungan akses concurrent bawaan pada DashMap juga sangat menguntungkan untuk aplikasi multi-threaded seperti BambangShop. Dengan demikian, meskipun Vec bisa digunakan, DashMap lebih unggul dalam hal kecepatan, efisiensi, dan kemudahan pemeliharaan.

  2. When programming using Rust, we are enforced by rigorous compiler constraints to make a thread-safe program. In the case of the List of Subscribers (SUBSCRIBERS) static variable, we used the DashMap external library for thread safe HashMap. Explain based on your understanding of design patterns, do we still need DashMap or we can implement Singleton pattern instead?

    Walaupun penggunaan Singleton (melalui lazy_static) memastikan hanya ada satu instance SUBSCRIBERS, hal tersebut tidak otomatis menjamin keamanan saat melakukan operasi concurrent pada HashMap. Singleton hanya menangani inisialisasi tunggal, sementara keamanan thread diperlukan untuk operasi baca/tulis yang aman antar-thread. DashMap, di sisi lain, menyediakan mekanisme thread-safe secara built-in tanpa perlu menambahkan lock manual seperti Mutex atau RwLock, yang bisa menambah kompleksitas dan risiko deadlock. Dengan demikian, untuk memastikan operasi yang efisien dan aman pada sistem multi-threaded seperti BambangShop, penggunaan DashMap tetap diperlukan, karena kedua konsep tersebut memiliki peran yang berbeda dan saling melengkapi.

Reflection Publisher-2

  1. In the Model-View Controller (MVC) compound pattern, there is no “Service” and “Repository”. Model in MVC covers both data storage and business logic. Explain based on your understanding of design principles, why we need to separate “Service” and “Repository” from a Model?

    Pemisahan antara Service dan Repository dari Model sangat penting untuk menerapkan prinsip Single Responsibility Principle (SRP) dalam pengembangan perangkat lunak. Berikut adalah beberapa alasan mengapa pemisahan tersebut diperlukan:

    • Tanggung Jawab Terpisah: Model sebaiknya hanya merepresentasikan struktur data, tanpa menyertakan logika bisnis atau detail akses penyimpanan. Repository bertugas mengelola akses data (misalnya operasi CRUD ke database atau API), sedangkan Service menangani logika bisnis dan validasi. Dengan pemisahan ini, setiap komponen memiliki satu tanggung jawab spesifik.

    • Modularitas dan Testabilitas: Dengan memisahkan Service dan Repository, masing-masing komponen dapat diuji secara terpisah. Hal ini meningkatkan modularitas dan mengurangi coupling antar bagian, sehingga perubahan pada salah satu komponen (misalnya perubahan struktur database) tidak akan langsung memengaruhi logika bisnis atau bagian lainnya.

    • Maintainability dan Scalability: Pemisahan ini mendukung clean coding dan memudahkan perawatan kode dalam jangka panjang. Setiap komponen dapat dikembangkan atau diganti secara independen, sehingga sistem menjadi lebih scalable dan fleksibel untuk pengembangan teknologi atau perbaikan di masa depan.

    Dengan demikian, meskipun Model dalam pola MVC mencakup data storage dan logika dasar, pemisahan Service dan Repository memberikan keuntungan dalam hal organisasi kode, kemudahan pengujian, dan pemeliharaan sistem secara keseluruhan.

  2. What happens if we only use the Model? Explain your imagination on how the interactions between each model (Program, Subscriber, Notification) affect the code complexity for each model?

    Jika kita hanya mengandalkan Model tanpa memisahkan Service dan Repository, setiap model seperti Program, Subscriber, dan Notification akan dipaksa menangani banyak tanggung jawab sekaligus (mulai dari penyimpanan data hingga logika bisnis). Hal ini menyebabkan pelanggaran prinsip Single Responsibility, di mana setiap komponen seharusnya fokus pada satu fungsi saja. Akibatnya, kode akan menjadi sangat kompleks dan terjalin erat (high coupling), sehingga perubahan kecil di salah satu bagian logika bisnis dapat memicu perubahan di struktur data, atau sebaliknya.

    Selain itu, karena Model harus menangani segala hal, ukurannya bisa menjadi sangat besar dan sulit dipahami, dimodifikasi, serta di-debug. Isolasi antar fungsi pun akan hilang, membuat unit testing menjadi tidak efektif karena tidak ada pemisahan yang jelas antara logika bisnis dan akses data. Secara keseluruhan, ketergantungan hanya pada Model akan meningkatkan kompleksitas, menurunkan maintainability, dan berpotensi meningkatkan risiko bug di sistem.

  3. Have you explored more about Postman? Tell us how this tool helps you to test your current work. You might want to also list which features in Postman you are interested in or feel like it is helpful to help your Group Project or any of your future software engineering projects.

    Postman sangat membantu dalam pengujian endpoint API yang telah dibuat. Dengan Postman, saya dapat mensimulasikan berbagai jenis request seperti GET, POST, PUT, dan DELETE serta dengan mudah mengatur parameter, header, dan body request untuk mengamati respons server. Fitur-fitur yang saya anggap sangat berguna meliputi HTTP Request Testing yang memungkinkan pengiriman berbagai jenis request secara fleksibel, Collections yang memudahkan pengelompokan endpoint dalam folder sehingga pengujian menjadi lebih terstruktur, Response Validation untuk memastikan bahwa respons yang diterima sesuai dengan ekspektasi, dan Environment Variables yang membantu menyimpan serta menggunakan variabel global di seluruh request agar pengujian bisa dilakukan di berbagai environment seperti development, staging, dan production. Dengan demikian, Postman tidak hanya mempercepat proses pengujian API, tetapi juga meningkatkan efisiensi pengembangan dan membantu meminimalkan risiko munculnya bug, sehingga sangat bermanfaat untuk proyek grup maupun proyek perangkat lunak di masa depan.

Reflection Publisher-3

=======

BambangShopMain

Getting started

To make it easy for you to get started with GitLab, here's a list of recommended next steps.

Already a pro? Just edit this README.md and make it your own. Want to make it easy? Use the template at the bottom!

Add your files

cd existing_repo
git remote add origin https://gitlab.cs.ui.ac.id/allan.kwek/bambangshopmain.git
git branch -M main
git push -uf origin main

Integrate with your tools

Collaborate with your team

Test and Deploy

Use the built-in continuous integration in GitLab.


Editing this README

When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to makeareadme.com for this template.

Suggestions for a good README

Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.

Name

Choose a self-explaining name for your project.

Description

Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.

Badges

On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.

Visuals

Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.

Installation

Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.

Usage

Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.

Support

Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.

Roadmap

If you have ideas for releases in the future, it is a good idea to list them in the README.

Contributing

State if you are open to contributions and what your requirements are for accepting them.

For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.

You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.

Authors and acknowledgment

Show your appreciation to those who have contributed to the project.

License

For open source projects, say how it is licensed.

Project status

If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.