lenra-io / server

GNU Affero General Public License v3.0
6 stars 0 forks source link
hacktoberfest

Contributors Forks Stargazers Issues AGPL License


Lenra Server

This repository the Lenra server application.

Report Bug · Request Feature

Getting Started

Prerequisites

You will first need to start two databases, postgres and mongo. Postgres will be used by the server to store general data and Mongo will store the data of the applications that you run. You can do this by using the docker compose up -d command at the root of this project.

Init git submodules : git submodule update --init --recursive

You will then need to install and setup elixir prerequisites for the server to run properly :

(back to top)

Start UP

Now you can start the server with this command mix phx.server

The server is started at localhost:4000

Code quality check :

Troubleshooting

Links

Some rules to respect the layer model

Use 3 layers :

For the naming, we use a singular name then we derive it (User, UserController, UserServices)

The Controller

Exemple

Simplified example of a "basic" controller: :

defmodule LenraWeb.PostController do
  use LenraWeb, :controller

  alias LenraWeb.Guardian.Plug
  alias Lenra.{PostServices}
  alias Lenra.{Repo}

  def index(conn, _params) do
    posts = PostServices.all()

    conn
    |> assign_data(posts)
    |> reply
  end

  def show(conn, params) do
    post = PostServices.get(params.id)

    conn
    |> assign_data(post)
    |> reply
  end

  def create(conn, params) do
    Plug.current_resource(conn)
    |> PostServices.add_post(params)
    |> Repo.transaction()
    |> case do
      {:ok, %{inserted_post: post}} -> 
        conn
        |> assign_data(post)
        |> reply
      {:error, {_, reason, _}} ->
        conn
        |> assign_error(reason)
        |> reply
      end
  end

  def update(conn, params) do
    PostServices.get(params.id)
    |> PostServices.update(params)
    |> Repo.transaction()
    |> case do
      {:ok, %{updated_post: post}} -> 
        conn
        |> assign_data(post)
        |> reply
      {:error, {_, reason, _}} ->
        conn
        |> assign_error(reason)
        |> reply
      end
  end
end

The Entity Model

It allows the creation/update of a data structure with help functions.

Exemple

Simplified example of "basic" model :

defmodule Post do
  use Ecto.Schema
  import Ecto.Changeset
  alias Lenra.User

  schema "posts" do
    field(:title, :string)
    field(:body, :string)
    belongs_to(:user, User)
    timestamps()
  end

  def changeset(post, params \\ %{}) do
    post
    |> cast(params, [:title, :body])
    |> validate_required([:title, :body])
    |> validate_length(:title, min: 3, max: 120)
    |> validate_length(:title, min: 10)
  end

  def new(user, params) do
    Ecto.build_assoc(user, :posts) # Création de l'association avec le user dans le new
    |> changeset(params) # création de l'objet + vérif des contraintes
  end

  def update(post, params) do
    post # Ici, pas d'association à mettre à jour
    |> changeset(params) # update de l'objet + vérif des contraintes
  end
end

The Service

It contains the business logic. It assumes that its entries have been verified. There are 2 main types of basic operation, reading and writing.

(back to top)

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please open an issue with the tag "enhancement" or "bug". Don't forget to give the project a star! Thanks again!

(back to top)

License

Distributed under the AGPL License. See LICENSE for more information.

(back to top)