Skip to content
Snippets Groups Projects
Commit 0077aa73 authored by Geovanny Avelar's avatar Geovanny Avelar
Browse files

Endpoint to retrieve images location and delete orphan on photos

parent 2ebd418e
No related branches found
Tags v2.11.31
No related merge requests found
Loading
Loading
@@ -7,7 +7,7 @@
 
<groupId>br.com.agenciacodeplus</groupId>
<artifactId>socialcron</artifactId>
<version>2.10.31</version>
<version>2.11.31</version>
<packaging>war</packaging>
 
<name>SocialCRON-API</name>
Loading
Loading
Loading
Loading
@@ -11,7 +11,7 @@ create table if not exists photos (
filename varchar(100) not null,
media_type varchar(50) not null,
post_id bigint(20),
foreign key (post_id) references posts(id)
foreign key (post_id) references posts(id) on delete cascade;
);
 
create table if not exists profiles (
Loading
Loading
package br.com.agenciacodeplus.socialcron.controllers;
 
import br.com.agenciacodeplus.socialcron.acl.ACLPermissions;
import br.com.agenciacodeplus.socialcron.exceptions.EmptyFileException;
import br.com.agenciacodeplus.socialcron.photos.Photo;
import br.com.agenciacodeplus.socialcron.photos.PhotosService;
import br.com.agenciacodeplus.socialcron.posts.Post;
import br.com.agenciacodeplus.socialcron.posts.PostsService;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
Loading
Loading
@@ -13,6 +7,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpStatus;
Loading
Loading
@@ -30,6 +27,13 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
import br.com.agenciacodeplus.socialcron.acl.ACLPermissions;
import br.com.agenciacodeplus.socialcron.exceptions.EmptyFileException;
import br.com.agenciacodeplus.socialcron.photos.Photo;
import br.com.agenciacodeplus.socialcron.photos.PhotosService;
import br.com.agenciacodeplus.socialcron.posts.Post;
import br.com.agenciacodeplus.socialcron.posts.PostsService;
@RestController
@RequestMapping(value = "/v2/photos")
public class PhotosController {
Loading
Loading
@@ -99,4 +103,27 @@ public class PhotosController {
.body(new InputStreamResource(fileStream));
}
// FIXME The for should be removed from controller. Bussiness logic cannot be here
@CrossOrigin
@RequestMapping(value = "/post/{id}", method = RequestMethod.GET)
@PreAuthorize(
"hasPermission(#id, 'br.com.agenciacodeplus.socialcron.posts.Post', 'read')")
public ResponseEntity<List<String>> findAll(@PathVariable Long id) {
Post post = postsService.findOne(id);
if(post == null) {
return new ResponseEntity<List<String>>(HttpStatus.NOT_FOUND);
}
List<Photo> photos = service.findByPost(post);
List<String> path = new LinkedList<String>();
for(Photo photo : photos) {
path.add("/v2/photos/" + photo.getId());
}
return ResponseEntity.ok(path);
}
}
package br.com.agenciacodeplus.socialcron.photos;
 
import br.com.agenciacodeplus.socialcron.exceptions.EmptyFileException;
import br.com.agenciacodeplus.socialcron.posts.Post;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
Loading
Loading
@@ -15,6 +15,7 @@ import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
Loading
Loading
@@ -22,6 +23,11 @@ import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.MultipartFile;
 
import com.fasterxml.jackson.annotation.JsonIgnore;
import br.com.agenciacodeplus.socialcron.exceptions.EmptyFileException;
import br.com.agenciacodeplus.socialcron.posts.Post;
@Entity
@Table(name = "photos")
@Component
Loading
Loading
@@ -37,12 +43,13 @@ public class Photo {
private MultipartFile file;
@Column(name = "filename")
@JsonIgnore
private String filename;
@Column(name = "media_type")
private String mediaType;
@ManyToOne
@ManyToOne(cascade = CascadeType.REMOVE)
@JoinColumn(name = "post_id", referencedColumnName = "ID")
private Post post;
 
Loading
Loading
@@ -69,7 +76,7 @@ public class Photo {
public void setFilename(String filename) {
this.filename = filename;
}
public String getMediaType() {
return mediaType;
}
Loading
Loading
package br.com.agenciacodeplus.socialcron.photos;
 
import br.com.agenciacodeplus.socialcron.posts.Post;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
 
@Repository
public interface PhotosRepository extends JpaRepository<Photo, Long> {
public List<Photo> findByPost(Post post);
}
package br.com.agenciacodeplus.socialcron.photos;
 
import br.com.agenciacodeplus.socialcron.posts.Post;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Loading
Loading
@@ -22,6 +23,10 @@ public class PhotosService {
return dao.findAll();
}
public List<Photo> findByPost(Post post) {
return dao.findByPost(post);
}
public void delete(Long id) {
dao.delete(id);
}
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment