public async Task CreateAsync_ShouldReturnCreatedCompany()
        {
            var company = new Company()
            {
                Name                 = "New created company",
                VatNumber            = "BE.0123.456.789",
                EstablishmentAddress = new Address()
                {
                    AddressLine1 = "AddressLine1",
                    AddressLine2 = "AddressLine2",
                    PostCode     = "PostCode",
                    City         = "City"
                }
            };

            Company result = null;

            try
            {
                result = await _repository.CreateAsync(company);

                Assert.IsType <Company>(result);
                Assert.NotEqual(Guid.Empty, result.Id);
            }
            finally
            {
                _storage.Companies.Remove(result.Id);
            }
        }
        public async Task CreateAsync_ShouldReturnCreatedCompany()
        {
            var company = new Company()
            {
                Name                 = "New created company",
                VatNumber            = "BE.0123.456.789",
                EstablishmentAddress = new Address()
                {
                    AddressLine1 = "AddressLine1",
                    AddressLine2 = "AddressLine2",
                    PostCode     = "PostCode",
                    City         = "City"
                }
            };

            Company result = null;

            try
            {
                result = await _repository.CreateAsync(company);

                Assert.IsType <Company>(result);
                Assert.NotEqual(Guid.Empty, result.Id);
            }
            finally
            {
                var companyEntityToDelete = await _context.Companies.FirstOrDefaultAsync(c => c.Id == result.Id);

                if (companyEntityToDelete != null)
                {
                    _context.Companies.Remove(companyEntityToDelete);
                    await _context.SaveChangesAsync();
                }
            }
        }
Example #3
0
        public async Task <Company> CreateCompanyAsync(Company company)
        {
            var exists = await CompanyRepository.GetFirstWhereAsync(c => c.Name == company.Name);

            if (exists != null)
            {
                throw new AlreadyExistsException("Компания с таким названием уже существует!");
            }

            company.CreatedOn = DateTime.Now;
            company.Owner     = await UserRepository.GetFirstWhereAsync(
                u => u.UserName == company.Owner.UserName);

            company.Owner.Company = company;
            company.CompanyRoles  = new List <CompanyRole>
            {
                CompanyRole.GetDefaultCompanyAdminRole(),
                    CompanyRole.GetDefaultCompanyOwnerRole()
            };

            var newCompany = await CompanyRepository.CreateAsync(company);

            await CompanyRepository.SaveAsync();

            await UserRepository.UpdateAsync(newCompany.Owner);

            await UserRepository.SaveAsync();

            await UserCompanyRolesRepository.CreateAsync(
                new UserCompanyRole
            {
                CompanyRole = company.CompanyRoles.FirstOrDefault(
                    r => r.Name == CompanyRole.OwnerRoleName),
                User = company.Owner
            });

            await UserCompanyRolesRepository.SaveAsync();

            return(newCompany);
        }
        public async Task <ActionResult> Create(Company model)
        {
            var _bizCompany = new CompanyRepository();
            var id          = await _bizCompany.CreateAsync(model);

            if (id > 0)
            {
                if (!string.IsNullOrWhiteSpace(model.LastNote))
                {
                    var note = new GhichuModel
                    {
                        Noidung = model.LastNote,
                        HosoId  = id,
                        TypeId  = (int)NoteType.Company,
                        UserId  = GlobalData.User.IDUser
                    };

                    await _rpNote.AddNoteAsync(note);
                }
                return(ToResponse(true));
            }
            return(ToResponse(false));
        }