You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Current »

have
package social.api;

import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import social.data.*;

import java.nio.ByteBuffer;
import java.util.Base64.Encoder;

@Controller
@RequestMapping(path="/mgr")
public class LinkGenerator {
	@Autowired // This means to get the bean called userRepository
    // Which is auto-generated by Spring, we will use it to handle the data
	private LinkRepository linkRepository;
	
	static int incNum = 0;
	
	protected String getUniqueKey() {					
		byte[] uniquyebytes = ByteBuffer.allocate(4).putInt(incNum).array();			
		String strHash = Base64.encodeBase64URLSafeString( uniquyebytes );				
		incNum++;
		return strHash;		
	}
	
	
	@GetMapping(path="/linkadd") // Map ONLY GET Requests
	public @ResponseBody String addNewLink (
	@RequestParam String uid
	, @RequestParam String cid
	, @RequestParam String renderurl
	, @RequestParam String imageurl
	, @RequestParam String title
	, @RequestParam String content) {
		
		//String hashStr = Long.toHexString( (uid+cid).hashCode() ).toUpperCase();	
		String hashStr = getUniqueKey();
		LinkInfo linkInfo = new LinkInfo();
		linkInfo.setUrlKey(hashStr);
		linkInfo.setCid(cid);
		linkInfo.setRenderurl(renderurl);
		linkInfo.setUid(uid);
		linkInfo.setImageurl(imageurl);
		linkInfo.setTitle(title);
		linkInfo.setContent(content);					
		linkRepository.save(linkInfo);
		
		return linkInfo.getUrlKey();
	}
	
	@GetMapping(path="/linkall")
	public @ResponseBody Iterable<LinkInfo> getAllUsers() {
		// This returns a JSON or XML with the users
		return linkRepository.findAll();
	}

}




data
package social.data;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity // This tells Hibernate to make a table out of this class
public class LinkInfo {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    private String urlkey;		//짧은 URL
    private String uid;			//셀러 식별아이디    
    private String cid;			//셀러상품 식별아이디    
    private String renderurl;	//이동페이지 Url    
    private String imageurl;	//메타정보
    private String title;
    private String content;
        
    
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUrlKey() {
		return urlkey;
	}

	public void setUrlKey(String urlkey) {
		this.urlkey = urlkey;
	}
	
	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}
	
	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}
	
	public String getRenderurl() {
		return renderurl;
	}

	public void setRenderurl(String renderurl) {
		this.renderurl = renderurl;
	}
	
	public String getImageurl() {
		return imageurl;
	}

	public void setImageurl(String imageurl) {
		this.imageurl = imageurl;
	}
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

}


package social.data;
import java.util.List;
import org.springframework.data.repository.CrudRepository;

import social.data.*;


public interface LinkRepository extends CrudRepository<LinkInfo, Long>{
	 
	List<LinkInfo> findByUrlkey(String urlkey);

}



  • No labels