package org.marsik.elshelves.backend.entities.converters;

import org.marsik.elshelves.api.entities.AuthorizationApiModel;
import org.marsik.elshelves.api.entities.UserApiModel;
import org.marsik.elshelves.backend.entities.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;
import java.util.UUID;

@Service
public class UserToEmber implements CachingConverter<User, UserApiModel, UUID> {
    @Autowired
    AuthorizationToEmber authorizationToEmber;

    @Override
    public UserApiModel convert(User entity, int nested, Map<UUID, Object> cache) {
        if (entity == null) {
            return null;
        }

        if (cache.containsKey(entity.getId())) {
            return (UserApiModel)cache.get(entity.getId());
        }

        UserApiModel user = new UserApiModel();
		if (nested > 0
				&& entity.getId() != null) {
			cache.put(entity.getId(), user);
		}

		return convert(entity, user, nested, cache);
    }

	@Override
	public UserApiModel convert(User entity, UserApiModel user, int nested, Map<UUID, Object> cache) {
		user.setId(entity.getId());

		if (nested == 0) {
			return user;
		}

		user.setEmail(entity.getEmail());
		user.setName(entity.getName());

        user.setAuthorizations(new ArrayList<AuthorizationApiModel>());
        /*if (entity.getAuthorizations() != null) {
            for (Authorization auth : entity.getAuthorizations()) {
                user.getAuthorizations().add(authorizationToEmber.convert(auth, nested - 1, cache));
            }
        }*/

		return user;
	}
}

@@ -163,32 +163,92 @@
 s.UserApiModel;%0A
+import org.marsik.elshelves.backend.entities.Authorization;%0A
 import org.marsi
@@ -1436,10 +1436,8 @@
     
-/*
 if (
@@ -1670,10 +1670,8 @@
    %7D
-*/
 %0A%0A%09%09

Add authorizations back to UserApiModel