001package com.nico.service;
002 
003import java.util.List;
004 
005import javax.transaction.Transactional;
006import javax.ws.rs.Consumes;
007import javax.ws.rs.DELETE;
008import javax.ws.rs.DefaultValue;
009import javax.ws.rs.GET;
010import javax.ws.rs.POST;
011import javax.ws.rs.PUT;
012import javax.ws.rs.Path;
013import javax.ws.rs.PathParam;
014import javax.ws.rs.Produces;
015import javax.ws.rs.core.MediaType;
016import javax.ws.rs.core.Response;
017 
018import org.springframework.beans.factory.annotation.Autowired;
019import org.springframework.stereotype.Component;
020 
021import com.nico.dao.AccountMapper;
022import com.nico.entities.Account;
023import com.nico.entities.PagerEx;
024 
025@Component
026@Path("/accounts")
027public class AccountService {
028    @Autowired
029    private AccountMapper accountMapper;
030     
031    @GET
032    @Produces({"application/json; charset=UTF-8","application/xml; charset=UTF-8"})
033    public List<Account> getAccounts() {
034        return accountMapper.getAccounts();
035    }
036     
037    @GET
038//  @Path("/query")  @QueryParam
039    @Path("{pageSize}/{pageIndex}")         // pageSize:每页记录数  pageIndex:当前页
040    @Produces({"application/json; charset=UTF-8","application/xml; charset=UTF-8"})
041    public List<Account> getAccountsByFilter(@DefaultValue("10") @PathParam("pageSize") int pageSize,@DefaultValue("1") @PathParam("pageIndex") int pageIndex) {
042        int startIndex = (pageIndex-1)*pageSize;    //开始l
043        int endIndex = pageSize ;                   //条目数
044        PagerEx pagerEx = new PagerEx();
045        pagerEx.setPageIndex(startIndex);
046        pagerEx.setPageSize(endIndex);
047        return accountMapper.getAccountsByFilter(pagerEx);
048    }
049     
050    @GET @Path("{accountID}")
051    @Produces({"application/json; charset=UTF-8","application/xml; charset=UTF-8"})
052    public Response getAccountByAccountID(@PathParam("accountID") String accountID) {
053        Account account = accountMapper.getAccountByAccountID(accountID);
054        if(account != null) {
055            return Response.status(200).entity(account).build();
056        } else {
057            return Response.status(404).entity("The Account with the accountID " + accountID + " does not exist").build();
058        }
059    }
060     
061    @DELETE @Path("{accountID}")
062    @Produces({MediaType.TEXT_HTML})
063    @Transactional
064    public Response deleteAccountByAccountID(@PathParam("accountID") String accountID) {
065        if(accountMapper.deleteAccountByAccountID(accountID) == 1){
066            return Response.status(204).build();
067        } else {
068            return Response.status(404).entity("Account with the AccountID " + accountID + " is not present in the database").build();
069        }
070    }
071     
072    @POST @Path("batch")
073    @Consumes({"application/json; charset=UTF-8"})
074    @Produces({MediaType.TEXT_HTML})
075    @Transactional
076    public Response deleteAccountsBatch(List<String> list) {
077        int num = accountMapper.deleteAccountsBatch(list);
078        if(num > 0) {
079            return Response.status(204).entity("Has deleted "+num+" Accounts").build();
080        }else {
081            return Response.status(404).entity("Accounts deleted failed").build();
082        }
083             
084    }
085     
086    @POST
087    @Consumes({"application/json; charset=UTF-8"})
088    @Produces({MediaType.TEXT_HTML})   
089    @Transactional
090    public Response createAccount(Account account) {
091        int flag = accountMapper.createAccount(account);
092        if(flag > 0){
093            return Response.status(201).entity("A new Account/Resource has been created").build();     
094        }else {
095            return Response.status(406).entity("A new Account/Resource create failed").build();        
096        }
097    }
098     
099     
100    @PUT
101    @Consumes({"application/json; charset=UTF-8"})
102    @Produces({MediaType.TEXT_HTML})   
103    @Transactional
104    public Response updateAccount(Account account) {
105        String message;
106        int status;
107        if(accountWasUpdated(account)){
108            status = 200;       //OK
109            message = "Account has been updated";
110        } else {
111            status = 406;       //Not acceptable
112            message = "The information you provided is not sufficient to perform either an UPDATE or "
113                    + " an INSERTION of the new hospital resource <br/>"
114                    + " If you want to UPDATE please make sure you provide an existent <strong>id</strong> <br/>"
115                    + " If you want to insert a new hospital please provide at least a <strong>title</strong> and the <strong>feed</strong> for the hospital resource";
116        }
117        return Response.status(status).entity(message).build();    
118    }
119     
120    private boolean accountWasUpdated(Account account) {
121        return accountMapper.updateAccountByAccountID(account)== 1;
122    }
123 
124}
最近下载更多
503382513  LV10 2022年12月5日
203778513  LV9 2021年7月21日
Myangyyyy  LV10 2021年3月11日
阿昌先生  LV13 2021年1月20日
心中无码  LV5 2021年1月7日
王东东  LV17 2020年11月4日
ggc110  LV1 2020年5月31日
会1飞1的1鱼  LV5 2020年4月16日
xaseven  LV1 2020年4月8日
micaroo  LV4 2019年11月26日
最近浏览更多
13188866605  LV12 2024年3月26日
忆丶流年似水 2023年1月16日
暂无贡献等级
康日澜  LV9 2023年1月7日
呜呜呜吴天  LV2 2022年12月9日
微信网友_6248713511227392  LV11 2022年12月5日
503382513  LV10 2022年12月5日
云龙123456  LV7 2022年12月2日
annazhang  LV29 2022年10月27日
葛乃玮  LV1 2022年10月19日
51Demo 2022年2月14日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友