package com.ilya.sergeev.potlach;

import java.security.Principal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ilya.sergeev.potlach.client.Touch;
import com.ilya.sergeev.potlach.client.TouchSvcApi;
import com.ilya.sergeev.potlach.client.UserInfo;
import com.ilya.sergeev.potlach.repository.TouchRepository;
import com.ilya.sergeev.potlach.repository.UserInfoRepository;

@Controller
public class TouchController
{
	@Autowired
	TouchRepository mTouchRepository;
	
	@Autowired
	UserInfoRepository mUserRepository;
	
	@PreAuthorize("hasRole('USER')")
	@RequestMapping(value = TouchSvcApi.TOUCH_PATH, method = RequestMethod.POST)
	public @ResponseBody
	Touch touch(@RequestParam(TouchSvcApi.GIFT_ID_PARAM) long giftId, Principal principal)
	{
		String userName = principal.getName();
		Touch touch = mTouchRepository.findOneByUserNameAndGiftId(userName, giftId);
		if (touch == null)
		{
			touch = new Touch(userName, giftId);
			touch = mTouchRepository.save(touch);
		}
		
		UserInfo user = mUserRepository.findByName(userName);
		user.setRating(user.getRating() + 1);
		
		return touch;
	}
}

@@ -487,16 +487,61 @@
 eBody;%0A%0A
+import com.ilya.sergeev.potlach.client.Gift;%0A
 import c
@@ -578,16 +578,16 @@
 .Touch;%0A
-
 import c
@@ -679,16 +679,75 @@
 erInfo;%0A
+import com.ilya.sergeev.potlach.repository.GiftRepository;%0A
 import c
@@ -1005,16 +1005,63 @@
 tory;%0A%09%0A
+%09@Autowired%0A%09GiftRepository mGiftRepository;%0A%09%0A
 %09@PreAut
@@ -1510,16 +1510,63 @@
 %0A%09%09%7D%0A%09%09%0A
+%09%09Gift gift = mGiftRepository.findOne(giftId);%0A
 %09%09UserIn
@@ -1602,24 +1602,31 @@
 dByName(
-userName
+gift.getOwner()
 );%0A%09%09use
@@ -1660,16 +1660,46 @@
 ) + 1);%0A
+%09%09mUserRepository.save(user);%0A
 %09%09%0A%09%09ret

fix bug in logic