Sangcheol Hwang 2013-01-14
refactoring user profile avatar
@1f25fe0df5b39454f5645507a9d864e467dbf1bb
app/controllers/UserApp.java
--- app/controllers/UserApp.java
+++ app/controllers/UserApp.java
@@ -160,6 +160,7 @@
 			return badRequest(signup.render("title.signup", newUserForm));
 		else {
 			User user = newUserForm.get();
+			user.avatarUrl = "/assets/images/default-avatar-128.png";
 			User.create(hashedPassword(user));
 
 			setUserInfoInSession(user);
@@ -236,10 +237,14 @@
         User user = UserApp.currentUser();
         user.email = userForm.data().get("email");
         user.name = userForm.data().get("name");
-        user.update();
-
+        
         Attachment.deleteAll(Resource.USER_AVATAR, currentUser().id);
-        Attachment.attachFiles(currentUser().id, null, Resource.USER_AVATAR, currentUser().id);
+        int attachFiles = Attachment.attachFiles(currentUser().id, null, Resource.USER_AVATAR, currentUser().id);
+        if(attachFiles>0) {
+        	user.avatarUrl = "/files/" + user.avatarId();
+        }
+    	
+        user.update();
         return redirect(routes.UserApp.userInfo(user.loginId));
     }
 
app/models/Attachment.java
--- app/models/Attachment.java
+++ app/models/Attachment.java
@@ -91,7 +91,7 @@
     }
 
     // Attach the files from the user's temporary area to the given container.
-    public static void attachFiles(
+    public static int attachFiles(
             Long userId, Long projectId, Resource containerType, Long containerId) {
         // Move the attached files in the temporary area to the issue area.
         List<Attachment> attachments = Attachment.findTempFiles(userId);
@@ -101,6 +101,7 @@
             attachment.containerId = containerId;
             attachment.save();
         }
+        return attachments.size();
     }
 
     // Store the files in the filesystem.
app/models/User.java
--- app/models/User.java
+++ app/models/User.java
@@ -131,16 +131,7 @@
                 .ne("projectUser.role.id", RoleType.SITEMANAGER.roleType())
                 .findList();
     }
-    
-    public static Long avatartIdByLoginId(String loginId) {
-    	return findByLoginId(loginId).avatarId();
-    }
-    
-    @Cached(key = "isCustomAvatar")
-    public boolean isCustomAvatar() {
-    	return Attachment.findByContainer(Resource.USER_AVATAR, id).size()>0 ? true : false;
-    }
-    
+        
     public Long avatarId(){
         return Attachment.findByContainer(Resource.USER_AVATAR, id).get(0).id;
     }
app/views/board/postList.scala.html
--- app/views/board/postList.scala.html
+++ app/views/board/postList.scala.html
@@ -66,7 +66,7 @@
             <i class="ico ico-comment-bubble"></i><span class="num">@post.commentCount</span>
         </div>
         <a href="@routes.UserApp.userInfo(post.authorLoginId)" class="author-avatar img-rounded pull-right">
-        <img class="user-picture" src="@urlToPicture(User.find.byId(post.authorId).email, 34)" alt="@post.authorName">
+        <img class="user-picture" src="@User.findByLoginId(session.get("loginId")).avatarUrl" width="34" height="34" alt="@post.authorName">
       </div>
     </li>
     }
app/views/user/edit.scala.html
--- app/views/user/edit.scala.html
+++ app/views/user/edit.scala.html
@@ -14,11 +14,7 @@
     <div class="bubble-wrap dark-gray project-home">
         <div class="inner logo">
             <div class="logo-wrap">
-                @if(user.isCustomAvatar()) {
-                <img src="@routes.AttachmentApp.getFile(user.avatarId())" width="34"/>
-                } else {
-                <img src="@routes.Assets.at("images/default-avatar-128.png")" />
-                }
+                <img src="@user.avatarUrl" />
             </div>
             <div id="picPath">업로드된 파일이 없습니다.</div>
                 <input id="attachmemt" type="file" name="filePath"/>
app/views/user/info.scala.html
--- app/views/user/info.scala.html
+++ app/views/user/info.scala.html
@@ -13,11 +13,7 @@
 	<div class="bubble-wrap dark-gray project-home">
 		<div class="inner logo">
             <div class="logo-wrap">
-                @if(user.isCustomAvatar()) {
-                <img src="@routes.AttachmentApp.getFile(user.avatarId())" />
-                } else {
-                <img src="@routes.Assets.at("images/default-avatar-128.png")" />
-                }
+                <img src="@user.avatarUrl" />
             </div>
         </div>
         <div class="inner project-info">
conf/initial-data.yml
--- conf/initial-data.yml
+++ conf/initial-data.yml
@@ -6,35 +6,41 @@
         password:       5v4TVjzLo1bqullT1CU4/bENUNOUfX97WpdunGLvJvw=
         passwordSalt:   '[B@1032a4'
         email:          admin@nhn.com
+        avatarUrl:      /assets/images/default-avatar-128.png
     - !!models.User
         name:           Hobi
         loginId:        hobi
         password:       ys9gr1Xet/DL9RpmgczOlJg+txPvqnZCaw/z55gb0KU=
         passwordSalt:   '[B@1032a4'        
         email:          hobi@nhn.com
+        avatarUrl:      /assets/images/default-avatar-128.png
     - !!models.User
         name:           scott
         loginId:        k16wire
         password:       v1O5ggs2TqM0prM1pLUD/zC+g7cavbWMXayVA9pvGHY=
         passwordSalt:   '[B@1032a4'        
         email:          k16wire@naver.com
+        avatarUrl:      /assets/images/default-avatar-128.png
     - !!models.User
         name:           suwon
         loginId:        doortts
         password:       rB5DBMe0hxMWs5FlQOJi5GmK+wj2Txx09MdgFqH83+k=
         passwordSalt:   '[B@1032a4'        
         email:          doortts@gmail.com
+        avatarUrl:      /assets/images/default-avatar-128.png
     - !!models.User
         name:           eungjun
         loginId:        nori
         password:       yOF0X0iKA1MVkaAJESNcdGE1n/3SWM8vDOKRYCXV2E0=
         passwordSalt:   '[B@1032a4'        
         email:          ejlee@nhn.com
+        avatarUrl:      /assets/images/default-avatar-128.png
     - !!models.User
         name:           anonymous
         loginId:        anonymous
         password:       
         email:          anonymous@nhn.com
+        avatarUrl:      /assets/images/default-avatar-128.png
         
 # Projects
 projects:
Add a comment
List