|
| 1 | +defmodule BlogEngine do |
| 2 | + defmodule Post do |
| 3 | + defstruct [:id, :title, :body, :created_at] |
| 4 | + end |
| 5 | + |
| 6 | + def start do |
| 7 | + IO.puts("Welcome to the Elixir Blog Engine CLI") |
| 8 | + loop([], 1) |
| 9 | + end |
| 10 | + |
| 11 | + defp loop(posts, next_id) do |
| 12 | + IO.puts("\nAvailable commands:") |
| 13 | + IO.puts(" new - Create a new post") |
| 14 | + IO.puts(" list - List all posts") |
| 15 | + IO.puts(" view [id] - View a post by ID") |
| 16 | + IO.puts(" delete [id] - Delete a post by ID") |
| 17 | + IO.puts(" quit - Exit the app") |
| 18 | + |
| 19 | + case IO.gets("\nCommand> ") |> String.trim() do |
| 20 | + "new" -> |
| 21 | + {post, new_id} = create_post(next_id) |
| 22 | + loop([post | posts], new_id) |
| 23 | + |
| 24 | + "list" -> |
| 25 | + list_posts(posts) |
| 26 | + loop(posts, next_id) |
| 27 | + |
| 28 | + "quit" -> |
| 29 | + IO.puts("Goodbye!") |
| 30 | + |
| 31 | + cmd when String.starts_with?(cmd, "view ") -> |
| 32 | + [_ | [id_str]] = String.split(cmd) |
| 33 | + view_post(posts, String.to_integer(id_str)) |
| 34 | + loop(posts, next_id) |
| 35 | + |
| 36 | + cmd when String.starts_with?(cmd, "delete ") -> |
| 37 | + [_ | [id_str]] = String.split(cmd) |
| 38 | + updated_posts = delete_post(posts, String.to_integer(id_str)) |
| 39 | + loop(updated_posts, next_id) |
| 40 | + |
| 41 | + _ -> |
| 42 | + IO.puts("Invalid command.") |
| 43 | + loop(posts, next_id) |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + defp create_post(id) do |
| 48 | + title = IO.gets("Title: ") |> String.trim() |
| 49 | + body = IO.gets("Body: ") |> String.trim() |
| 50 | + created_at = DateTime.utc_now() |> DateTime.to_string() |
| 51 | + |
| 52 | + post = %Post{ |
| 53 | + id: id, |
| 54 | + title: title, |
| 55 | + body: body, |
| 56 | + created_at: created_at |
| 57 | + } |
| 58 | + |
| 59 | + IO.puts("Post created with ID #{id}.") |
| 60 | + {post, id + 1} |
| 61 | + end |
| 62 | + |
| 63 | + defp list_posts([]) do |
| 64 | + IO.puts("No posts available.") |
| 65 | + end |
| 66 | + |
| 67 | + defp list_posts(posts) do |
| 68 | + IO.puts("\nPosts:") |
| 69 | + Enum.each(Enum.reverse(posts), fn post -> |
| 70 | + IO.puts(" [#{post.id}] #{post.title} (#{post.created_at})") |
| 71 | + end) |
| 72 | + end |
| 73 | + |
| 74 | + defp view_post(posts, id) do |
| 75 | + case Enum.find(posts, fn p -> p.id == id end) do |
| 76 | + nil -> |
| 77 | + IO.puts("Post not found.") |
| 78 | + post -> |
| 79 | + IO.puts("\n--- Post ##{post.id} ---") |
| 80 | + IO.puts("Title: #{post.title}") |
| 81 | + IO.puts("Created: #{post.created_at}") |
| 82 | + IO.puts("Body:\n#{post.body}") |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + defp delete_post(posts, id) do |
| 87 | + case Enum.find(posts, fn p -> p.id == id end) do |
| 88 | + nil -> |
| 89 | + IO.puts("Post not found.") |
| 90 | + posts |
| 91 | + _ -> |
| 92 | + IO.puts("Post #{id} deleted.") |
| 93 | + Enum.reject(posts, fn p -> p.id == id end) |
| 94 | + end |
| 95 | + end |
| 96 | +end |
| 97 | + |
| 98 | +BlogEngine.start() |
0 commit comments