001 | package com.nico.service; |
003 | import java.util.List; |
005 | import javax.transaction.Transactional; |
006 | import javax.ws.rs.Consumes; |
007 | import javax.ws.rs.DELETE; |
008 | import javax.ws.rs.DefaultValue; |
009 | import javax.ws.rs.GET; |
010 | import javax.ws.rs.POST; |
011 | import javax.ws.rs.PUT; |
012 | import javax.ws.rs.Path; |
013 | import javax.ws.rs.PathParam; |
014 | import javax.ws.rs.Produces; |
015 | import javax.ws.rs.core.MediaType; |
016 | import javax.ws.rs.core.Response; |
018 | import org.springframework.beans.factory.annotation.Autowired; |
019 | import org.springframework.stereotype.Component; |
021 | import com.nico.dao.AccountMapper; |
022 | import com.nico.entities.Account; |
023 | import com.nico.entities.PagerEx; |
027 | public class AccountService { |
029 | private AccountMapper accountMapper; |
032 | @Produces ({ "application/json; charset=UTF-8" , "application/xml; charset=UTF-8" }) |
033 | public List<Account> getAccounts() { |
034 | return accountMapper.getAccounts(); |
039 | @Path ( "{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; |
043 | int endIndex = pageSize ; |
044 | PagerEx pagerEx = new PagerEx(); |
045 | pagerEx.setPageIndex(startIndex); |
046 | pagerEx.setPageSize(endIndex); |
047 | return accountMapper.getAccountsByFilter(pagerEx); |
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(); |
057 | return Response.status( 404 ).entity( "The Account with the accountID " + accountID + " does not exist" ).build(); |
061 | @DELETE @Path ( "{accountID}" ) |
062 | @Produces ({MediaType.TEXT_HTML}) |
064 | public Response deleteAccountByAccountID( @PathParam ( "accountID" ) String accountID) { |
065 | if (accountMapper.deleteAccountByAccountID(accountID) == 1 ){ |
066 | return Response.status( 204 ).build(); |
068 | return Response.status( 404 ).entity( "Account with the AccountID " + accountID + " is not present in the database" ).build(); |
073 | @Consumes ({ "application/json; charset=UTF-8" }) |
074 | @Produces ({MediaType.TEXT_HTML}) |
076 | public Response deleteAccountsBatch(List<String> list) { |
077 | int num = accountMapper.deleteAccountsBatch(list); |
079 | return Response.status( 204 ).entity( "Has deleted " +num+ " Accounts" ).build(); |
081 | return Response.status( 404 ).entity( "Accounts deleted failed" ).build(); |
087 | @Consumes ({ "application/json; charset=UTF-8" }) |
088 | @Produces ({MediaType.TEXT_HTML}) |
090 | public Response createAccount(Account account) { |
091 | int flag = accountMapper.createAccount(account); |
093 | return Response.status( 201 ).entity( "A new Account/Resource has been created" ).build(); |
095 | return Response.status( 406 ).entity( "A new Account/Resource create failed" ).build(); |
101 | @Consumes ({ "application/json; charset=UTF-8" }) |
102 | @Produces ({MediaType.TEXT_HTML}) |
104 | public Response updateAccount(Account account) { |
107 | if (accountWasUpdated(account)){ |
109 | message = "Account has been updated" ; |
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" ; |
117 | return Response.status(status).entity(message).build(); |
120 | private boolean accountWasUpdated(Account account) { |
121 | return accountMapper.updateAccountByAccountID(account)== 1 ; |