public async Task <ApplicationResponseDto> CreateApplicationAsync(Guid user, int nameId)
        {
            // TODO: create a dto including the IndustrySector
            // Getting the reserved name
            var name = await _context.Names
                       .Include(n => n.NameSearch)
                       .ThenInclude(n => n.Application)
                       .ThenInclude(a => a.SortingOffice)
                       .SingleAsync(n => n.EntityNameId.Equals(nameId));

            // Constructing a new Application object
            var application =
                new Application(user, EService.PrivateLimitedCompany, EApplicationStatus.Incomplete,
                                name.NameSearch.Application.SortingOffice.CityId);

            // Constructing a new Private entity and associating with the application
            var privateEntity = new Fridge.Models.Main.PrivateEntity(name);

            privateEntity.MemorandumOfAssociation = new MemorandumOfAssociation();
            application.PrivateEntity             = privateEntity;


            bool resubmission = false;

            // Mark name as used
            if (name.Status == ENameStatus.Used)
            {
                // await _context.Entry(name).Collection(n => n.PrivateEntities).Query().Include(p => p.CurrentApplication)
                //     .LoadAsync();
                // if (name.PrivateEntities.Count > 0)
                // {
                //     var entity = name.PrivateEntities.First();
                // }
                resubmission = true;
            }
            else
            {
                name.Status = ENameStatus.Used;
            }


            // Mark application for insertion
            await _context.AddAsync(application);

            return(await ReturnApplicationResponse(application));
        }
        public async Task <SubmittedNameSearchResponseDto> CreateNewNameSearchAsync(Guid user,
                                                                                    NewNameSearchRequestDto dto)
        {
            var application =
                new Application(user, EService.NameSearch, EApplicationStatus.Submitted, dto.SortingOffice);
            var nameSearch = _mapper.Map <NewNameSearchRequestDto, Fridge.Models.Main.NameSearch>(dto);

            application.NameSearch = nameSearch;
            var transaction = await _context.Database.BeginTransactionAsync();

            try
            {
                await _context.AddAsync(application);

                await _context.SaveChangesAsync();

                application.NameSearch.Reference = $"NS/{nameSearch.NameSearchId}";

                Guid payment;
                try
                {
                    payment = await _paymentsService.BillAsync(EService.NameSearch, user, nameSearch.Reference);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }

                application.Payment = payment;
                await _context.SaveChangesAsync();

                await transaction.CommitAsync();
            }
            catch (Exception e)
            {
                await transaction.RollbackAsync();

                throw;
            }

            // TODO: verify if SaveChangesSuccessful
            return(new SubmittedNameSearchResponseDto
            {
                Id = application.ApplicationId,
                NameSearch = nameSearch.NameSearchId,
                Reference = nameSearch.Reference
            });
        }