public async Task <ApiResponse <CustomerInfoDTO> > Login(CustomerLoginDTO loginRequest)
        {
            ApiResponse <CustomerInfoDTO> result = new ApiResponse <CustomerInfoDTO>();

            try
            {
                Customer customer = await unitOfWork.CustomersManager.GetCustomerByPhoneNumber(loginRequest.MobileNumber);

                if (customer != null)
                {
                    if (customer.TotalNumberOfVisits == null)
                    {
                        customer.TotalNumberOfVisits = 1;
                        customer.LastVisitDate       = DateTime.Now;
                    }
                    else
                    {
                        customer.TotalNumberOfVisits++;
                        customer.LastVisitDate = DateTime.Now;
                    }


                    await unitOfWork.SaveChangesAsync();

                    CustomerInfoDTO customerInfo = new CustomerInfoDTO();
                    customerInfo.Id                  = customer.Id;
                    customerInfo.Name                = customer.Name;
                    customerInfo.LastBarberId        = customer.LastBarberId;
                    customerInfo.LastVisitDate       = customer.LastVisitDate;
                    customerInfo.TotalNumberOfVisits = customer.TotalNumberOfVisits;
                    customerInfo.PhoneNumber         = customer.PhoneNumber;

                    result.Data      = customerInfo;
                    result.Succeeded = true;
                    return(result);
                }
                result.Succeeded = false;
                result.Errors.Add("Phone number not registered , Kindly create a new account !");
                result.ErrorType = ErrorType.LogicalError;
                return(result);
            }
            catch (Exception ex)
            {
                result.Succeeded = false;
                result.Errors.Add(ex.Message);
                return(result);
            }
        }
예제 #2
0
        private async Task <ServiceResult <CustomerInfoDTO> > GetCustomerTransactionsInfoAsync(Customer customer, int takeTransactions)
        {
            var result = new ServiceResult <CustomerInfoDTO>();

            if (customer == null)
            {
                result.Status = ServiceResultStatus.NotFound;
                return(result);
            }

            var transactions = await _context.Transactions
                               .Where(t => t.CustomerID == customer.CustomerID)
                               .OrderByDescending(t => t.TransactionTime)
                               .Take(takeTransactions)
                               .ToListAsync();

            var transactionsDTO = transactions.Select(t => TransactionInfoDTO.MapFromDomain(t)).ToList();

            result.Data   = CustomerInfoDTO.MapFromDomain(customer, transactionsDTO);
            result.Status = ServiceResultStatus.Success;

            return(result);
        }
        public async Task <ApiResponse <CustomerInfoDTO> > GuestLogin()
        {
            ApiResponse <CustomerInfoDTO> result = new ApiResponse <CustomerInfoDTO>();

            try
            {
                var currentGuestNumberResult = await unitOfWork.GuestNumberManager.GetAsync();

                List <GuestNumber> currentGuestNumberList = currentGuestNumberResult.ToList();

                if (currentGuestNumberList.Count == 0)
                {
                    GuestNumber initialGuestNumber = new GuestNumber();

                    initialGuestNumber.CurrentGuestNumber = 0;


                    var guestNumberResult = await unitOfWork.GuestNumberManager.CreateAsync(initialGuestNumber);

                    await unitOfWork.SaveChangesAsync();

                    if (guestNumberResult != null)
                    {
                        GuestNumber currentGuestNumber = guestNumberResult;

                        Customer customer = new Customer();

                        customer.Name = "Guest " + currentGuestNumber.CurrentGuestNumber.ToString();

                        customer.LastVisitDate = DateTime.Now;

                        customer.PhoneNumber = "00000000";

                        customer.Email = "Guest";

                        customer.TotalNumberOfVisits = 1;

                        var customerResult = await unitOfWork.CustomersManager.CreateAsync(customer);

                        var guestNumberUpdateResult = await unitOfWork.GuestNumberManager.GetByIdAsync(currentGuestNumber.Id);

                        guestNumberUpdateResult.CurrentGuestNumber++;

                        var res = await unitOfWork.SaveChangesAsync();

                        CustomerInfoDTO customerInfoToReturn = new CustomerInfoDTO();

                        customerInfoToReturn.Id            = customer.Id;
                        customerInfoToReturn.Name          = customer.Name;
                        customerInfoToReturn.LastVisitDate = customer.LastVisitDate;

                        if (res == true)
                        {
                            result.Data      = customerInfoToReturn;
                            result.Data.Name = customerResult.Name;
                            result.Succeeded = true;
                            return(result);
                        }
                        else
                        {
                            result.Succeeded = false;
                            result.Errors.Add("Failed to login as guest !");
                            result.ErrorType = ErrorType.LogicalError;
                            return(result);
                        }
                    }
                    else
                    {
                        result.Succeeded = false;
                        result.Errors.Add("Failed to initialize guest number !");
                        result.ErrorType = ErrorType.LogicalError;
                        return(result);
                    }
                }
                else
                {
                    GuestNumber currentGuestNumber = currentGuestNumberList[0];

                    Customer customer = new Customer();

                    customer.Name = "Guest " + currentGuestNumber.CurrentGuestNumber.ToString();

                    customer.LastVisitDate = DateTime.Now;

                    customer.PhoneNumber = "*******";

                    customer.Email = "*******";

                    customer.TotalNumberOfVisits = 1;

                    var customerResult = await unitOfWork.CustomersManager.CreateAsync(customer);

                    var guestNumberUpdateResult = await unitOfWork.GuestNumberManager.GetByIdAsync(currentGuestNumber.Id);

                    guestNumberUpdateResult.CurrentGuestNumber++;

                    var res = await unitOfWork.SaveChangesAsync();

                    CustomerInfoDTO customerInfoToReturn = new CustomerInfoDTO();

                    customerInfoToReturn.Id            = customer.Id;
                    customerInfoToReturn.Name          = customer.Name;
                    customerInfoToReturn.LastVisitDate = customer.LastVisitDate;

                    if (res == true)
                    {
                        result.Data      = customerInfoToReturn;
                        result.Data.Name = customerResult.Name;
                        result.Succeeded = true;
                        return(result);
                    }
                    else
                    {
                        result.Succeeded = false;
                        result.Errors.Add("Failed to login as guest !");
                        result.ErrorType = ErrorType.LogicalError;
                        return(result);
                    }
                }
            }
            catch (Exception ex)
            {
                result.Succeeded = false;
                result.Errors.Add(ex.Message);
                return(result);
            }
        }