package cn.momia.mapi.api.v1.feed;

import cn.momia.api.feed.FeedServiceApi;
import cn.momia.api.feed.dto.FeedDto;
import cn.momia.api.user.UserServiceApi;
import cn.momia.api.user.dto.UserDto;
import cn.momia.common.api.dto.PagedList;
import cn.momia.common.api.http.MomiaHttpResponse;
import cn.momia.common.webapp.config.Configuration;
import cn.momia.image.api.ImageFile;
import cn.momia.mapi.api.v1.AbstractV1Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import java.util.List;

@RestController
@RequestMapping("/v1/feed")
public class FeedV1Api extends AbstractV1Api {
    @Autowired private FeedServiceApi feedServiceApi;
    @Autowired private UserServiceApi userServiceApi;

    @RequestMapping(method = RequestMethod.GET)
    public MomiaHttpResponse list(@RequestParam(required = false, defaultValue = "") String utoken, @RequestParam int start) {
        if (start < 0) return MomiaHttpResponse.BAD_REQUEST;

        long userId = 0;
        if (!StringUtils.isBlank(utoken)) {
            UserDto user = userServiceApi.get(utoken);
            userId = user.getId();
        }

        PagedList<FeedDto> pagedFeeds = feedServiceApi.list(userId, start, Configuration.getInt("PageSize.Feed"));
        processFeeds(pagedFeeds.getList());

        return MomiaHttpResponse.SUCCESS(pagedFeeds);
    }

    private void processFeeds(List<FeedDto> feeds) {
        for (FeedDto feed : feeds) {
            List<String> imgs = feed.getImgs();
            feed.setImgs(completeMiddleImgs(imgs));
            feed.setLargeImgs(completeLargeImgs(imgs));
            feed.setAvatar(ImageFile.smallUrl(feed.getAvatar()));
        }
    }
}

@@ -1949,11 +1949,510 @@
  %7D%0A    %7D
+%0A%0A    @RequestMapping(value = %22/delete%22, method = RequestMethod.POST)%0A    public MomiaHttpResponse delete(@RequestParam String utoken, @RequestParam long id) %7B%0A        if (StringUtils.isBlank(utoken)) return MomiaHttpResponse.TOKEN_EXPIRED;%0A        if (id %3C= 0) return MomiaHttpResponse.BAD_REQUEST;%0A%0A        UserDto user = userServiceApi.get(utoken);%0A        if (!feedServiceApi.delete(user.getId(), id)) return MomiaHttpResponse.FAILED(%22%E5%88%A0%E9%99%A4Feed%E5%A4%B1%E8%B4%A5%22);%0A%0A        return MomiaHttpResponse.SUCCESS;%0A    %7D
 %0A%7D%0A

delete feed