Skip to content

Commit

Permalink
Update podcast and using exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ismael G Marin C committed Feb 13, 2020
1 parent 9ea4d36 commit 88fcf25
Show file tree
Hide file tree
Showing 12 changed files with 364 additions and 35 deletions.
16 changes: 16 additions & 0 deletions app/Controllers/Http/HomeController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";

const Podcast = use("App/Models/Podcast");

class HomeController {
async index({ view }) {
const podcasts = await Podcast.query()
.orderBy("id", "desc")
.with("category")
.fetch();

return view.render("home", { podcasts: podcasts.toJSON() });
}
}

module.exports = HomeController;
92 changes: 86 additions & 6 deletions app/Controllers/Http/PodcastController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Podcast = use("App/Models/Podcast");
const { validateAll } = use("Validator");
const Helpers = use("Helpers");
const uuid = require("uuid/v4");
const UnauthorizedException = use("App/Exceptions/UnauthorizedException");

class PodcastController {
async create({ view }) {
Expand All @@ -28,19 +29,18 @@ class PodcastController {
}

const user = auth.user;
const logo = request.file("logo", {
types: ["image"],
size: "2mb"
});
const logo = await this._processLogoUpload(request);

await logo.move(Helpers.publicPath("uploads/logos"), {
name: `${uuid()}.${logo.subtype}`
});

if (!logo.moved()) {
session.flash({
type: "danger",
message: logo.error().message
notification: {
type: danger,
message: logo.error().message
}
});

return response.redirect("back");
Expand All @@ -60,6 +60,86 @@ class PodcastController {

return response.route("myPodcast");
}

async edit({ view, params }) {
const podcast = await Podcast.findOrFail(params.id);
const categories = await Category.pair("id", "name");

return view.render("podcasts/edit", { podcast, categories });
}

async update({ params, request, response, session, auth }) {
const data = request.only([
"title",
"category_id",
"description",
"user_id"
]);
const podcast = await Podcast.findOrFail(params.id);

const rules = {
title: "required",
category_id: "required",
description: "required"
};

const validation = await validateAll(data, rules);

if (auth.user.id !== podcast.user_id) {
throw new UnauthorizedException("You can only edit your podcast", 403);
}

if (validation.fails()) {
session.withErrors(validation.messages());

return response.redirect("back");
}

if (request.file("logo").size > 0) {
const logo = await this._processLogoUpload(request);

if (!logo.moved()) {
session.flash({
notification: {
type: danger,
message: logo.error().message
}
});

return response.redirect("back");
}

podcast.logo = `uploads/logos/${logo.fileName}`;
}

podcast.title = data.title;
podcast.category_id = data.category_id;
podcast.description = data.description;

await podcast.save();

session.flash({
notification: {
type: "success",
message: "Podcast updated!"
}
});

return response.route("myPodcast");
}

async _processLogoUpload(request) {
const logo = request.file("logo", {
types: ["image"],
size: "2mb"
});

await logo.move(Helpers.publicPath("uploads/logos"), {
name: `${uuid()}.${logo.subtype}`
});

return logo;
}
}

module.exports = PodcastController;
13 changes: 0 additions & 13 deletions app/Controllers/Http/TestController.js

This file was deleted.

6 changes: 6 additions & 0 deletions app/Controllers/Http/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
const { validateAll } = use("Validator");
const Hash = use("Hash");
class UserController {
async myPodcast({ view, auth }) {
const podcast = await auth.user.podcast().fetch();

return view.render("users.my_podcast", { podcast });
}

show({ view }) {
return view.render("users.show");
}
Expand Down
23 changes: 23 additions & 0 deletions app/Exceptions/UnauthorizedException.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";

const { LogicalException } = require("@adonisjs/generic-exceptions");

class UnauthorizedException extends LogicalException {
/**
* Handle this exception by itself
*/
async handle(error, { response, session }) {
session.flash({
notification: {
type: "danger",
message: error.message
}
});

await session.commit();

return response.route("myPodcast");
}
}

module.exports = UnauthorizedException;
2 changes: 1 addition & 1 deletion app/Models/Podcast.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Podcast extends Model {
return this.hasMany("App/Models/Episode");
}
category() {
return this.hasMany("App/Models/Category");
return this.belongsTo("App/Models/Category");
}

subscribers() {
Expand Down
5 changes: 3 additions & 2 deletions app/Validators/Podcast.js → app/Validators/StorePodcast.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";
const Podcast = use("App/Models/Podcast");

class Podcast {
class StorePodcast {
get validateAll() {
return true;
}
Expand All @@ -14,4 +15,4 @@ class Podcast {
}
}

module.exports = Podcast;
module.exports = StorePodcast;
44 changes: 44 additions & 0 deletions resources/views/home.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
@layout('layouts.app')

@section('content')
@set('title', 'Your all round dev podcast')

<section class="section">
<div class="container">
<h2 class="subtitle is-3">Latest podcasts</h2>

<div class="columns is-multiline">
@each(podcast in podcasts)
<div class="column is-one-quarter">
<div class="card">
<div class="card-image">
<figure class="image is-3by2">
<a href="{{ route('podcasts.show', { slug: podcast.slug }) }}">
<img src="{{ assetsUrl(podcast.logo) }}" alt="Podcast logo">
</a>
</figure>
</div>
<div class="card-content">
<div class="content">
<a href="{{ route('podcasts.show', { slug: podcast.slug }) }}">
{{ podcast.title }}
</a>

<div>
<a href="{{ route('categories.show', { slug: podcast.category.slug }) }}">
<span class="tag">
{{ podcast.category.name }}
</span>
</a>
</div>
</div>
</div>
</div>
</div>
@else
<p>No podcast to display.</p>
@endeach
</div>
</div>
</section>
@endsection
113 changes: 113 additions & 0 deletions resources/views/podcasts/edit.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
@layout('layouts.app')

@section('content')
@set('title', 'Edit podcast')

<section class="section">
<div class="container">
<div class="columns">
<div class="column is-three-fifths is-offset-one-fifth">
<h2 class="title has-text-centered">Edit podcast</h2>

@include('layouts.partials._notification')

<form action="{{ '/podcasts/' + podcast.id + '?_method=PUT' }}" method="post" enctype="multipart/form-data">
{{ csrfField() }}

<div class="field is-horizontal">
<div class="field-label">
<label class="label">Title</label>
</div>
<div class="field-body">
<div class="field">
<div class="control">
<input
type="text"
class="input"
name="title"
value="{{ old('title', podcast.title) }}"
placeholder="Podcast title">
</div>

{{ elIf('<p class="help is-danger">$self</p>', getErrorFor('title'), hasErrorFor('title')) }}
</div>
</div>
</div>

<div class="field is-horizontal">
<div class="field-label">
<label class="label">Category</label>
</div>
<div class="field-body">
<div class="field">
<div class="control">
<div class="select is-fullwidth">
<select name="category_id">
<option value="">Choose a category</option>
@each((name, id) in categories)
<option value="{{ id }}" {{ podcast.category_id == id ? 'selected': '' }}>{{ name }}</option>
@endeach
</select>
</div>
</div>

{{ elIf('<p class="help is-danger">$self</p>', getErrorFor('category'), hasErrorFor('category')) }}
</div>
</div>
</div>

<div class="field is-horizontal">
<div class="field-label">
<label class="label">Logo</label>
</div>
<div class="field-body">
<div class="field">
<div class="control">
<input
type="file"
class="input"
name="logo">
</div>

{{ elIf('<p class="help is-danger">$self</p>', getErrorFor('logo'), hasErrorFor('logo')) }}
</div>
</div>
</div>

<div class="field is-horizontal">
<div class="field-label">
<label class="label">Description</label>
</div>
<div class="field-body">
<div class="field">
<div class="control">
<textarea
class="textarea"
name="description"
rows="10"
placeholder="Give your podcast a description">{{ old('description', podcast.description) }}</textarea>
</div>

{{ elIf('<p class="help is-danger">$self</p>', getErrorFor('description'), hasErrorFor('description')) }}
</div>
</div>
</div>

<div class="field is-horizontal">
<div class="field-label"></div>
<div class="field-body">
<div class="field">
<div class="control">
<button class="button is-primary">
Update podcast
</button>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
@endsection
Loading

0 comments on commit 88fcf25

Please sign in to comment.