Skip to content

Instantly share code, notes, and snippets.

@maxdemarzi maxdemarzi/Post.java
Created Aug 27, 2018

Embed
What would you like to do?
<li class="media list-group-item p-4">
<form id="status" enctype="multipart/form-data" action="/post" method="POST" style="width: 100%">
<div class="input-group">
<input name="status" type="text" class="form-control" placeholder="What's happening?" autofocus>
<div class="input-group-btn">
<button type="button" class="btn btn-secondary align-self-stretch">
<span class="icon icon-camera"></span>
</button>
</div>
<input name="file" type="file"/>
</div>
</form>
</li>
package com.maxdemarzi.models;
import humanize.Humanize;
import lombok.Data;
import org.jooby.Upload;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
@Data
public class Post {
private Long id;
private String status;
private String name;
private String name2;
private String username;
private String username2;
private String filename;
private Upload file;
private String hash;
private String hash2;
private String time;
private Integer low_fives;
private boolean low_fived;
private Integer high_fives;
private boolean high_fived;
private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy");
public String when() {
ZonedDateTime dateTime = ZonedDateTime.parse(time);
return dateFormat.format(dateTime);
}
public String humanTime() {
return Humanize.naturalTime(Date.from(ZonedDateTime.parse(time).toInstant()));
}
public String expires() {
return Humanize.naturalTime(Date.from(ZonedDateTime.parse(time).plusDays(5).toInstant()));
}
}
public class Posts extends Jooby {
private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss_SSS");
public Posts() {
super("posts");
}
{
post("/post", req -> {
Post post = req.form(Post.class);
CommonProfile profile = require(CommonProfile.class);
String username = profile.getUsername();
String filename = post.getFilename();
Upload upload = req.file(filename);
RequestBody requestBody = RequestBody.create(MultipartBody.FORM, upload.file());
String time = dateFormat.format(ZonedDateTime.now());
Response<ResponseBody> bunnyResponse = App.bunny.upload("fives", username, time, requestBody).execute();
if (bunnyResponse.isSuccessful()) {
post.setFilename(username+"/"+time);
Response<Post> response = App.api.createPost(username, post).execute();
if (response.isSuccessful()) {
return Results.redirect("/home");
}
}
throw new Err(Status.BAD_REQUEST);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.