# Content Database

Content services in bringin backend project use grpc server to serve logic. The grpc protocol defined at **api/proto** folder.

Content services serve logics like ( create content, generate feed, vote content, unvote content & comment content)**.**&#x20;

<figure><img src="https://101487635-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FludJfnevuurEmnRhlTBb%2Fuploads%2FAzg4dooU0KUrOd8n1Zal%2FScreen%20Shot%202023-06-30%20at%2011.35.57.png?alt=media&#x26;token=47816039-8811-47e5-b069-583fae947733" alt=""><figcaption></figcaption></figure>

### Tech Stack

* Golang version 1.20
* Postgres version 14 as Primary Database
* Redis for subscribing data from user services

### Installation

* Make sure you have golang version 1.20
* Make sure you have postgres that support uuid, jsonb
* Make sure you have installed redis
* See Recommmended project structure section.

### Migrate Database

Make sure you already create **social network database**. Look at folder **internal/sqls** you can execute sql in that folder to migrate database. You must to migrate tables at **social network database.**

<figure><img src="https://101487635-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FludJfnevuurEmnRhlTBb%2Fuploads%2FG8KD9Rhxg1STfU5bgwUo%2FScreen%20Shot%202023-06-30%20at%2011.36.08.png?alt=media&#x26;token=ab808081-d082-483b-bf00-7c3c04ae7ac0" alt=""><figcaption></figcaption></figure>

Besides executing the commands above, you can execute the command below to migrate database. But before you execute migrate command, make sure you already set database environment in **.env** file

<pre class="language-go"><code class="lang-go"><strong>    go mod download // run this if dependency not downloaded
</strong>    go mod tidy // run this if dependency changed
    go run cmd/main.go -command=migrate:up // execute migration up will create tables
</code></pre>

There are 3 migrate commands :

* command `migrate:up` will create tables
* command `migrate:down` will drop tables
* command `migrate:both` will drop and then create tables

### Run Services

First to run this services, make sure your database and the tables that needed already created. Then create **.env** file from **example.env** template. The **example.env** file look likes below change the configuration and match with your environment.

For **CONTENT\_DATABASE\_HOST,  CONTENT\_DATABASE\_PORT, CONTENT\_DATABASE\_NAME, CONTENT\_DATABASE\_USER & CONTENT\_DATABASE\_PASSWORD** must be same with social network services database environment. Why this ? Because to generate feed, i have to join activity\_models table (content services) with user\_network\_models (Social network services)

<figure><img src="https://101487635-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FludJfnevuurEmnRhlTBb%2Fuploads%2FxnrfcroZDKPKAYugTywE%2FScreen%20Shot%202023-06-30%20at%2011.31.51.png?alt=media&#x26;token=58706ad5-0c49-4425-9d82-60fb606af37a" alt=""><figcaption></figcaption></figure>

After **.env** file created. Run this command

```go
    go mod download // run this if dependency not downloaded
    go mod tidy // run this if dependency changed
    go run cmd/main.go -command=main
```
