Permalink
Browse files

*Release

  • Loading branch information...
1 parent 89e44a2 commit 6029644f7a75419c16074e2465881975f3652a68 Jonathan Garay committed Feb 12, 2012
Showing with 1,657 additions and 293 deletions.
  1. +18 −0 pom.xml
  2. +31 −0 src/main/java/net/devmask/tuit/FileUtils.java
  3. +4 −3 src/main/java/net/devmask/tuit/TuitInitializer.java
  4. +26 −0 src/main/java/net/devmask/tuit/TuitRepository.java
  5. +3 −1 src/main/java/net/devmask/tuit/TuitSession.java
  6. +45 −0 src/main/java/net/devmask/tuit/UserRepository.java
  7. +34 −0 src/main/java/net/devmask/tuit/controllers/DashboardController.java
  8. +46 −0 src/main/java/net/devmask/tuit/controllers/FollowController.java
  9. +3 −3 src/main/java/net/devmask/tuit/controllers/SessionController.java
  10. +57 −0 src/main/java/net/devmask/tuit/controllers/TuitsController.java
  11. +17 −83 src/main/java/net/devmask/tuit/controllers/UsersController.java
  12. +68 −0 src/main/java/net/devmask/tuit/decorators/TuitDecorator.java
  13. +3 −1 src/main/java/net/devmask/tuit/models/Tuit.java
  14. +13 −3 src/main/java/net/devmask/tuit/models/User.java
  15. +1 −1 src/main/resources/META-INF/persistence.xml
  16. +6 −3 src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml
  17. +20 −17 src/main/webapp/WEB-INF/spring/root-context.xml
  18. +23 −8 src/main/webapp/WEB-INF/views/accounts/new.jsp
  19. +2 −0 src/main/webapp/WEB-INF/views/home.jsp
  20. +51 −8 src/main/webapp/WEB-INF/views/users/dashboard.jsp
  21. BIN src/main/webapp/resources/img/no_avatar_perfil.jpg
  22. +864 −0 src/main/webapp/resources/js/jquery-form.js
  23. +89 −8 src/main/webapp/resources/js/tuit.dashboard.backbone.js
  24. +3 −1 tuit.iml
  25. +30 −5 tuit.ipr
  26. +200 −148 tuit.iws
View
18 pom.xml
@@ -51,6 +51,12 @@
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-tx</artifactId>
+ <version>${spring.version}</version>
+ </dependency>
+
<!--Web tier-->
<dependency>
<groupId>opensymphony</groupId>
@@ -140,6 +146,18 @@
<version>${commons-dbcp.version}</version>
</dependency>
+ <!-- File Upload -->
+ <dependency>
+ <groupId>commons-fileupload</groupId>
+ <artifactId>commons-fileupload</artifactId>
+ <version>1.2.2</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>2.0.1</version>
+ </dependency>
+
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
@@ -0,0 +1,31 @@
+package net.devmask.tuit;
+
+import org.springframework.web.context.request.WebRequest;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+/**
+ * @author <a href="mailto:jonathan@devmask.net"> Jonathan Garay </a>
+ * 11/02/12 Creado
+ */
+public class FileUtils {
+
+ public static boolean storeFile(MultipartFile file, String path){
+ try {
+ File o = new File(".");
+ FileOutputStream fos = new FileOutputStream(o.getCanonicalPath()+path+file.getOriginalFilename());
+ fos.write(file.getBytes());
+ } catch (IOException e) {
+ e.printStackTrace();
+ return false;
+ }
+ return true;
+ }
+
+ public static boolean storeImage(MultipartFile file){
+ return storeFile(file,"/target/tuit-1.0-SNAPSHOT/resources/img");
+ }
+}
@@ -1,10 +1,9 @@
package net.devmask.tuit;
-import org.springframework.web.WebApplicationInitializer;
-import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
-
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
+import org.springframework.web.WebApplicationInitializer;
+import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
/**
* @author <a href="mailto:jonathan@devmask.net"> Jonathan Garay </a>
@@ -17,4 +16,6 @@ public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(WebConfig.class);
}
+
+
}
@@ -1,29 +1,55 @@
package net.devmask.tuit;
import net.devmask.tuit.models.Tuit;
+import net.devmask.tuit.models.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
+import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext ;
+import java.util.Date;
import java.util.List;
/**
* @author <a href="mailto:jonathan@devmask.net"> Jonathan Garay </a>
* 11/02/12 Creado
*/
@Repository
+@SuppressWarnings("unchecked")
public class TuitRepository {
@Autowired
private EntityManager entityManager;
+ @Transactional
public List<Tuit> findAll(){
return (List<Tuit>)entityManager.createNamedQuery("all.tuits")
.setMaxResults(10)
.getResultList();
}
+ public List<Tuit> findUserTimeline(User user){
+ return entityManager.createNamedQuery("user.timeline")
+ .setParameter("user",user)
+ .getResultList();
+ }
+
+ @Transactional
+ public Tuit createUserTuit(User user, Tuit tuit){
+ entityManager.getTransaction().begin();
+ tuit.setUser(user);
+ entityManager.persist(tuit);
+ entityManager.getTransaction().commit();
+ return tuit;
+ }
+
+ public List<Tuit> findTimelineSicen(User user, Date since){
+ return (List<Tuit>) entityManager.createNamedQuery("user.timeline.since")
+ .setParameter("user",user)
+ .setParameter("date",since)
+ .getResultList();
+ }
}
@@ -50,7 +50,9 @@ public void setLogedIn(Boolean logedIn) {
}
public Date getLastQuery() {
- return lastQuery;
+ Date last = lastQuery;
+ lastQuery = new Date(); // ugly
+ return last;
}
public void setLastQuery(Date lastQuery) {
@@ -2,9 +2,13 @@
import net.devmask.tuit.models.User;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.stereotype.Repository;
+import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
+import javax.transaction.TransactionManager;
+import java.util.List;
/**
* @author <a href="mailto:jonathan@devmask.net"> Jonathan Garay </a>
@@ -15,11 +19,52 @@
@Autowired
private EntityManager entityManager;
+
public User findLogin(User user){
return (User)entityManager.createNamedQuery("user.find.to_login")
.setParameter("username", user.getUsername())
.setParameter("password", user.getPassword())
.getSingleResult();
}
+
+ @Transactional
+ public void create(User user){
+ entityManager.getTransaction().begin();
+ entityManager.persist(user);
+ entityManager.getTransaction().commit(); //i don
+ }
+
+ public List<User> findUsersLike(String username){
+ return (List<User>)entityManager.createNamedQuery("user.by_username.like")
+ .setParameter("username","%"+username+"%")
+ .getResultList();
+ }
+
+ public User findByUsername(String username){
+ return (User)entityManager.createNamedQuery("user.by_username.like")
+ .setParameter("username",username) //hacky
+ .getSingleResult();
+ }
+
+ @Transactional
+ public User followUser(User follower, String tofollow ){
+ User user = findByUsername(tofollow);
+ System.out.println(user.getUsername());
+ System.out.println(follower.getUsername());
+ entityManager.getTransaction().begin();
+ user.getFollowers().add(follower);
+ follower.getFollowing().add(user);
+ entityManager.getTransaction().commit();
+ return user;
+ }
+
+ @Transactional
+ public User unfollowUser(User follower, String toUnfollow){
+ entityManager.getTransaction().begin();
+ follower.getFollowing().remove(findByUsername(toUnfollow));
+ entityManager.getTransaction().commit();
+ return follower;
+ }
+
}
@@ -0,0 +1,34 @@
+package net.devmask.tuit.controllers;
+
+import net.devmask.tuit.TuitRepository;
+import net.devmask.tuit.TuitSession;
+import net.devmask.tuit.models.Tuit;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+
+/**
+ * @author <a href="mailto:jonathan@devmask.net"> Jonathan Garay </a>
+ * 11/02/12 Creado
+ */
+@Controller
+@RequestMapping("/user/dashboard")
+public class DashboardController {
+
+ @Autowired
+ private TuitSession tuitSession;
+
+ @Autowired
+ private TuitRepository tuitRepository;
+
+ @RequestMapping(method = RequestMethod.GET)
+ public String dashboard(Model model){
+ model.addAttribute(new Tuit());
+ model.addAttribute("user",tuitSession.getCurrentUser());
+ model.addAttribute("timeline",tuitRepository.findUserTimeline(tuitSession.getCurrentUser()));
+ return "users/dashboard";
+ }
+
+}
@@ -0,0 +1,46 @@
+package net.devmask.tuit.controllers;
+
+import net.devmask.tuit.TuitSession;
+import net.devmask.tuit.UserRepository;
+import net.devmask.tuit.models.User;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import java.util.List;
+
+/**
+ * @author <a href="mailto:jonathan@devmask.net"> Jonathan Garay </a>
+ * 11/02/12 Creado
+ */
+@Controller
+@RequestMapping("/user/follow")
+public class FollowController {
+
+ @Autowired
+ private TuitSession tuitSession;
+
+ @Autowired
+ private UserRepository userRepository;
+
+ @RequestMapping(method = RequestMethod.GET)
+ public @ResponseBody
+ List<User> following(){
+ return tuitSession.getCurrentUser().getFollowing();
+ }
+
+ @RequestMapping(value = "{username}", method = RequestMethod.POST)
+ public @ResponseBody User toFollow(@PathVariable("username") String username){
+ return userRepository.followUser(tuitSession.getCurrentUser(),username);
+ }
+
+ @RequestMapping(value = "{username}", method = RequestMethod.DELETE)
+ public @ResponseBody User delete(@PathVariable("username") String username){
+ return userRepository.unfollowUser(tuitSession.getCurrentUser(),username);
+ }
+
+
+}
@@ -29,20 +29,20 @@
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(User user, Model model, RedirectAttributes redirectAttributes) {
- User loginUser = repository.findLogin(user);
+ tuitSession.login(repository.findLogin(user));
return "redirect:/user/dashboard";
}
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logout(Model model,RedirectAttributes redirectAttributes) {
tuitSession.logout();
- redirectAttributes.addFlashAttribute("success","Loged in. ");
+ redirectAttributes.addFlashAttribute("success","Logged out in. ");
return "redirect:/user/dashboard";
}
@ExceptionHandler
public String handle(NoResultException e, Model model) {
model.addAttribute("error","Email or password incorrect try again");
- return "redirect:user:home";
+ return "redirect:/users/home";
}
}
@@ -0,0 +1,57 @@
+package net.devmask.tuit.controllers;
+
+import net.devmask.tuit.TuitRepository;
+import net.devmask.tuit.TuitSession;
+import net.devmask.tuit.UserRepository;
+import net.devmask.tuit.decorators.TuitDecorator;
+import net.devmask.tuit.models.Tuit;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:jonathan@devmask.net"> Jonathan Garay </a>
+ * 11/02/12 Creado
+ */
+@Controller
+@RequestMapping("/user/tuits")
+public class TuitsController {
+ @Autowired
+ private TuitSession tuitSession;
+
+ @Autowired
+ private UserRepository userRepository;
+
+ @Autowired
+ private TuitRepository tuitRepository;
+
+ @RequestMapping(method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
+ @ResponseStatus(HttpStatus.CREATED)
+ public @ResponseBody TuitDecorator newTuit(@RequestBody TuitDecorator tuit){
+ return TuitDecorator.apply(tuitRepository.createUserTuit(tuitSession.getCurrentUser(), TuitDecorator.toTuit(tuit)),
+ tuitSession.getCurrentUser());
+ }
+
+ @RequestMapping(method = RequestMethod.GET)
+ public @ResponseBody List<TuitDecorator> tuits(Model model){
+ List<TuitDecorator> decorated = new ArrayList<TuitDecorator>(); // pew
+ for(Tuit tuti: tuitRepository.findUserTimeline(tuitSession.getCurrentUser())){
+ decorated.add(TuitDecorator.apply(tuti,tuitSession.getCurrentUser()));
+ }
+ return decorated;
+ }
+
+ @RequestMapping(value="/last", method = RequestMethod.GET)
+ public @ResponseBody List<TuitDecorator> lastTuits(Model model){
+ List<TuitDecorator> decorated = new ArrayList<TuitDecorator>(); // pew
+ for(Tuit tuti: tuitRepository.findTimelineSicen(tuitSession.getCurrentUser(), tuitSession.getLastQuery())){
+ decorated.add(TuitDecorator.apply(tuti,tuitSession.getCurrentUser()));
+ }
+ return decorated;
+ }
+}
Oops, something went wrong.

0 comments on commit 6029644

Please sign in to comment.