Search
 
SCRIPT & CODE EXAMPLE
 

PHP

spring delete objest from database that are not in your object list

Optional<Order> orderFromDb = orderRepo.findById(id);

    if(CollectionUtils.isNotEmpty(orderRequest.getItems()))

    {
        // if there are less items in update request than database
        if (orderRequest.getItems().size() < orderFromDb.getItems().size()) {
            Set<Long> itemIds = orderRequest.getItems().stream().map(id -> id.getId()).collect(Collectors.toSet());
            for (ItemRequest itemRequest : orderRequest.getItems()) {
                Iterator<Item> item = orderFromDb.getItems().iterator();
                Item i;
                while (item.hasNext()) {
                    i = item.next();
                    if (!itemIds.contains(i.getId())) {
                        item.remove();
                        continue;
                    }
                    if (i.getId() == itemRequest.getId()) {
                        i.setName(itemRequest.getName());
                    }
                }
            }
        } else {
            // if there are more or same items in update request and database
            for (ItemRequest itemRequest : orderRequest.getItems()) {
                // assuming for newly added items id will not be there(db should generate)
                if (itemRequest.getId() == null) {
                    Item item = new Item();
                    item.setName(itemRequest.getName());
                    item.setOrder(orderFromDb);
                    orderFromDb.getItems().add(item);
                    continue;
                }
                for (Item item : orderFromDb.getItems()) {
                    if (item.getId() == itemRequest.getId()) {
                        item.setName(request.getName());
                    }
                }
            }
        }
    }
Comment

PREVIOUS NEXT
Code Example
Php :: php accounting ledger 
Php :: index.php when deploying 
Php :: laravel create pivot migration 
Php :: require and include difference in laravel 
Php :: 500 Internal Server Error mamp rest api PDO 
Php :: laravel telescope redirect to localhost 
Php :: if laravel pagiantion not found error occured then 
Php :: Modal Edit Specific/Same table row, where button is 
Php :: laravel sendgrid using 2 possible authenticators. Authenticator LOGIN returned Expected response code 250 
Php :: how to type casting and overriding in php 
Php :: why are my css properties not being applied to php file 
Php :: php string concat 
Php :: eager load relationships on an existing model in route laravel 
Php :: php find longest string in array 
Php :: list database table rows plugin wordpress 
Php :: Program to Multiply Two Numbers in php 
Php :: Call to undefined method :last() 
Php :: print select sql result in php 
Php :: customize+forgot+password+laravel 
Php :: wp register_setting access saved value 
Php :: how to show limited text in laravel on page 
Php :: php get epoch timestamp of date 
Php :: php get cosine sim 
Php :: phpdoc array type 
Php :: php group subarrays by column key 
Php :: haseeb php code 
Php :: laravel short collection by keys based on array 
Php :: he PHP exec() function must be enabled. 
Php :: how to write a php program for an electricity bill using if-else conditions 
Php :: laravel route group within a group 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =