Giter Club home page Giter Club logo

discuss's Introduction

Discuss

To start your Phoenix server:

  • Run mix deps.get to install dependencies
  • Start Phoenix endpoint with mix phx.server or inside IEx with iex -S mix phx.server

Now you can visit localhost:4000/topics from your browser.

Learn more

  • This is mainly done running mix phoenix.new discuss
  • It will scaffold the project, creating a basic configuration for things like username and password for the database
  • It will also create a basic web UI, and a sample page controller with associated view

Setup

  • You need to run mix ecto.create: it will create the database using the parameters inside config folder. Ecto is a toolkit for data mapping and language integrated query for Elixir. You can see it as a wrapper for the database
  • Then with mix ecto.gen.migration add_topics you will create the first migration. A migration file is used to define changes to the database schema, such as creating or altering tables, adding or removing columns, and other modifications
  • Running mix ecto.migrate will actually mutate the database structure

Create Topic Model

  • Create Topic Model file. We just need a title String, the changeset fun is "boilerplate" to validate data before inserting into database, we receive a basic struct and we validate it

Index (List of Topics)

  • In the router define the entry get "/", TopicController, :index. This will invoke the fun index of TopicController everytime you receive a GET request at / path
  • Create the TopicController. For the index fun the controller gets all the Topics using Repo (from Ecto) and passes them to a view. render is the function to render a given view, with parameters. Here the view is :index, so it will load lib/discuss_web/controllers/topic_html/index.html.heex and we pass all the topics
  • The view will use some special markup provided by phoenix to render a header, an action and a table. Using ~p"/topics/... urls are validated at compile time to avoid errors

New (Create a new Topic)

Edit (Update a Topic)

Delete (Remove a Topic)

Models

Controller

Plugs

  • A plug is like an interceptor, executed for every request
  • Let's start with DiscussWeb.Plugs.SetUser. You look for :user_id in the session, and you add the associated user to the conn object, so you always have the current logged user available. To "enable" this plug, set it up in the router
  • Define another plug, DiscussWeb.Plugs.RequireAuth that will redirect the user to the index, if not logged and use inside TopicController
  • Create another plug, this time directly inside TopicController because it is the only user: check_topic_owner. This one will check if the requested topic was created by the logged user

Final changes

  • Now that a topic has an associated user, when creating it we must associate the current logged user with build_assoc. This is used by Ecto to manage relationships
  • Now you can update some links (1, 2) checking the user in the session

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.