// Convert a list of domain objects into protocol format with pagination support func ToDomainsResponse( domains []model.Domain, pagination dao.DomainDAOPagination, expand bool, filter string, ) DomainsResponse { var domainsResponses []DomainResponse for _, domain := range domains { domainsResponses = append(domainsResponses, ToDomainResponse(domain, true)) } var orderBy string for _, sort := range pagination.OrderBy { if len(orderBy) > 0 { orderBy += "@" } orderBy += fmt.Sprintf("%s:%s", dao.DomainDAOOrderByFieldToString(sort.Field), dao.DAOOrderByDirectionToString(sort.Direction), ) } expandParameter := "" if expand { expandParameter = "&expand" } // Add pagination managment links to the response. The URI is hard coded, I didn't have // any idea on how can we do this dynamically yet. We cannot get the URI from the // handler because we are going to have a cross-reference problem var links []Link // Only add fast backward if we aren't in the first page if pagination.Page > 1 { links = append(links, Link{ Types: []LinkType{LinkTypeFirst}, HRef: fmt.Sprintf("/domains/?pagesize=%d&page=%d&orderby=%s&filter=%s%s", pagination.PageSize, 1, orderBy, filter, expandParameter), }) } // Only add previous if theres a previous page if pagination.Page-1 >= 1 { links = append(links, Link{ Types: []LinkType{LinkTypePrev}, HRef: fmt.Sprintf("/domains/?pagesize=%d&page=%d&orderby=%s&filter=%s%s", pagination.PageSize, pagination.Page-1, orderBy, filter, expandParameter), }) } // Only add next if there's a next page if pagination.Page+1 <= pagination.NumberOfPages { links = append(links, Link{ Types: []LinkType{LinkTypeNext}, HRef: fmt.Sprintf("/domains/?pagesize=%d&page=%d&orderby=%s&filter=%s%s", pagination.PageSize, pagination.Page+1, orderBy, filter, expandParameter), }) } // Only add the fast forward if we aren't on the last page if pagination.Page < pagination.NumberOfPages { links = append(links, Link{ Types: []LinkType{LinkTypeLast}, HRef: fmt.Sprintf("/domains/?pagesize=%d&page=%d&orderby=%s&filter=%s%s", pagination.PageSize, pagination.NumberOfPages, orderBy, filter, expandParameter), }) } return DomainsResponse{ Page: pagination.Page, PageSize: pagination.PageSize, NumberOfPages: pagination.NumberOfPages, NumberOfItems: pagination.NumberOfItems, Domains: domainsResponses, Links: links, } }
// Convert a list of scan objects into protocol format with pagination support func ScansToScansResponse(scans []model.Scan, pagination dao.ScanDAOPagination) ScansResponse { var scansResponses []ScanResponse for _, scan := range scans { scansResponses = append(scansResponses, ScanToScanResponse(scan)) } var orderBy string for _, sort := range pagination.OrderBy { if len(orderBy) > 0 { orderBy += "@" } orderBy += fmt.Sprintf("%s:%s", dao.ScanDAOOrderByFieldToString(sort.Field), dao.DAOOrderByDirectionToString(sort.Direction), ) } // Add pagination managment links to the response. The URI is hard coded, I didn't have // any idea on how can we do this dynamically yet. We cannot get the URI from the // handler because we are going to have a cross-reference problem links := []Link{ { Types: []LinkType{LinkTypeCurrent}, HRef: "/scans/?current", }, } // Only add fast backward if we aren't in the first page if pagination.Page > 1 { links = append(links, Link{ Types: []LinkType{LinkTypeFirst}, HRef: fmt.Sprintf("/scans/?pagesize=%d&page=%d&orderby=%s", pagination.PageSize, 1, orderBy, ), }) } // Only add previous if theres a previous page if pagination.Page-1 >= 1 { links = append(links, Link{ Types: []LinkType{LinkTypePrev}, HRef: fmt.Sprintf("/scans/?pagesize=%d&page=%d&orderby=%s", pagination.PageSize, pagination.Page-1, orderBy, ), }) } // Only add next if there's a next page if pagination.Page+1 <= pagination.NumberOfPages { links = append(links, Link{ Types: []LinkType{LinkTypeNext}, HRef: fmt.Sprintf("/scans/?pagesize=%d&page=%d&orderby=%s", pagination.PageSize, pagination.Page+1, orderBy, ), }) } // Only add the fast forward if we aren't on the last page if pagination.Page < pagination.NumberOfPages { links = append(links, Link{ Types: []LinkType{LinkTypeLast}, HRef: fmt.Sprintf("/scans/?pagesize=%d&page=%d&orderby=%s", pagination.PageSize, pagination.NumberOfPages, orderBy, ), }) } return ScansResponse{ Page: pagination.Page, PageSize: pagination.PageSize, NumberOfPages: pagination.NumberOfPages, NumberOfItems: pagination.NumberOfItems, Scans: scansResponses, Links: links, } }