public async Task <CommandResponse> Handle(SetAliasForApplianceRequest request, CancellationToken cancellationToken)
        {
            if (!_validator.Validate(request).IsValid)
            {
                return(new CommandResponse(new Error("InvalidRequest", "Parameters are incorrect.")));
            }

            var applianceRegistered =
                await _unitOfWork.Repository.IsApplianceRegisteredWithCustomer(request.CustomerGuid, request.ApplianceId);

            if (applianceRegistered.Entity)
            {
                return(new CommandResponse(await _unitOfWork.Repository.SetAliasForCustomerAppliance(request.CustomerGuid,
                                                                                                     request.ApplianceId, request.Alias)));
            }

            var customerTask  = _unitOfWork.Repository.GetByGuidAsync(request.CustomerGuid, $"{nameof(Customer.Appliances)}");
            var applianceTask = _applianceService.GetCustomerAppliance(request.CustomerGuid, request.ApplianceId);

            await Task.WhenAll(customerTask, applianceTask);

            var appliance = applianceTask.Result;
            var customer  = customerTask.Result;

            if (appliance == null)
            {
                return(new CommandResponse(new Error("InvalidRequest", "Could not find remote registered appliance with given id.")));
            }

            var typeId = appliance.ApplianceTypeId;

            var applianceType = await _apUnitOfWork.Repository.SingleOrDefaultAsync(x => x.ApplianceTypeId == typeId);

            // TODO: Get actual name from API and store in ApplianceType
            applianceType ??= new ApplianceType(typeId, null);

            customer.Appliances.Add(new CustomerAppliance(request.ApplianceId, request.Alias, customer.Id, applianceType));

            try
            {
                await _unitOfWork.SaveChangesAsync();
            }
            catch (Exception)
            {
                return(new CommandResponse(new Error("ServerError", "Could not update Appliance Alias.")));
            }

            return(new CommandResponse());
        }
        public async Task <CommandResponse> Handle(SetApplianceCategoryRequest request, CancellationToken cancellationToken)
        {
            if (!_validator.Validate(request).IsValid)
            {
                return(new CommandResponse(new Error("InvalidRequest", "Unable to process request.")));
            }

            var customerTask = _unitOfWork.Repository.GetByGuidAsync(request.CustomerGuid,
                                                                     $"{nameof(Customer.Appliances)},{nameof(Customer.ApplianceCategories)}");

            var applianceTask = _service.GetCustomerAppliance(request.CustomerGuid, request.ApplianceId);

            await Task.WhenAll(customerTask, applianceTask);

            var customer  = customerTask.Result;
            var appliance = applianceTask.Result;

            if (appliance == null)
            {
                return(new CommandResponse(new Error("NoSuchAppliance", "Could not find requested Appliance.")));
            }

            if (request.CategoryName == null && customer.HasAppliance(request.ApplianceId))
            {
                if (!customer.ResetApplianceCategory(request.ApplianceId))
                {
                    return(new CommandResponse(new Error("ServerError", "ServerError")));
                }
            }

            else
            {
                if (!customer.HasApplianceCategory(request.CategoryName))
                {
                    return(new CommandResponse(new Error("NoSuchCategory",
                                                         $"No category: {request.CategoryName} is registered.")));
                }

                if (customer.HasAppliance(request.ApplianceId))
                {
                    if (!customer.SetApplianceCategory(request.ApplianceId, request.CategoryName))
                    {
                        return(new CommandResponse(new Error("ServerError", "Could not set category name.")));
                    }
                }

                else
                {
                    var applianceType =
                        await _atUnitOfWork.Repository.SingleOrDefaultAsync(x =>
                                                                            x.ApplianceTypeId == appliance.ApplianceTypeId);

                    var customerAppliance = new CustomerAppliance(request.ApplianceId, appliance.ApplianceName,
                                                                  customer.Id,
                                                                  applianceType ?? new ApplianceType(appliance.ApplianceTypeId, null));

                    customer.AddAppliance(customerAppliance);

                    if (!customer.SetApplianceCategory(request.ApplianceId, request.CategoryName))
                    {
                        return(new CommandResponse(new Error("ServerError", "Could not set category name.")));
                    }
                }
            }

            try
            {
                await _unitOfWork.SaveChangesAsync();

                return(new CommandResponse());
            }

            catch (Exception)
            {
                return(new CommandResponse(new Error("ServerError", "ServerError")));
            }
        }