Esempio n. 1
0
 public CompanyService(IMapper mapper, ICompanyRepository repository, IUnitOfWork unitOfWork)
 {
     _updateDeleteValidationRules = new UpdateCompanyValidator();
     _createValidationRules       = new CreateCompanyValidator();
     _unitOfWork = unitOfWork;
     _repository = repository;
     _mapper     = mapper;
 }
Esempio n. 2
0
        public async Task<IActionResult> Handle(UpdateCompanyCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var validator = new UpdateCompanyValidator();
                var result = validator.Validate(request.Dto);
                if (!result.IsValid)
                {
                    var errorMsg = "";

                    foreach (var err in result.Errors)
                    {
                        errorMsg += err.ErrorMessage + "\r\n";
                    }

                    return new JsonResult(new ApiResponse<object>
                    {
                        Code = 406,
                        ErrorMessage = errorMsg,
                        Data = null
                    });
                }

                if (!(await PermissionMonitor.CheckPermissionsAsync(_userRepository, request.UserId, "Panel administracji")))
                {
                    return new JsonResult(new ApiResponse<object>
                    {
                        Data = null,
                        Code = 403,
                        ErrorMessage = "Brak uprawnień"
                    });
                }

                var company = new Company
                {
                    Address = new Address
                    {
                        ApartmentNumber = request.Dto.Address.ApartmentNumber,
                        City = request.Dto.Address.City,
                        HouseNumber = request.Dto.Address.HouseNumber,
                        Id = request.Dto.Address.Id,
                        PostCode = request.Dto.Address.PostCode,
                        Province = request.Dto.Address.Province,
                        Street = request.Dto.Address.Street
                    },
                    Id = request.Dto.Id,
                    CompanyName = request.Dto.Name,
                    NIP = request.Dto.Nip,
                    Regon = request.Dto.Regon
                };

                await _companyRepository.UpdateCompanyAsync(company);

                return new JsonResult(new ApiResponse<object>
                {
                    Code = 201,
                    Data = null,
                    ErrorMessage = ""
                });
            }
            catch
            {
                return new JsonResult(new ApiResponse<object>
                {
                    Code = 500,
                    Data = null,
                    ErrorMessage = "Nastąpił problem z serwerem, skontaktuj się z działem IT."
                });
            }
        }