public async Task <ApiResult> Add([FromBody] SiteInput siteInput)
        {
            var model = _mapper.Map <Site>(siteInput);
            var res   = await _siteService.AddAsync(model);

            return(new ApiResult(data: res));
        }
Ejemplo n.º 2
0
            public async Task <SiteDto> Handle(Query request, CancellationToken cancellationToken)
            {
                var currentUser = await userManager.FindByIdAsync(userAccessor.GetCurrentUserId());

                if (await userManager.IsInRoleAsync(currentUser, RoleNames.Inspector))
                {
                    throw new RestException(HttpStatusCode.Forbidden, new { Forbidden = "Permission Denied." });
                }
                var doesNameExist = siteService.CheckExistsByName(request.Name);

                if (doesNameExist)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Name = "Site name already exists." });
                }
                var doesAddressExist = siteService.CheckExistsByAddress(request.Address);

                if (doesAddressExist)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Address = "Site address already exists." });
                }

                var newSite = new Site
                {
                    CompanyId   = request.CompanyId,
                    CompanyName = await companyService.GetCompanyNameByIdAsync(request.CompanyId),
                    Name        = request.Name,
                    Address     = request.Address,
                    Notes       = request.Notes,
                    CreatedOn   = DateTime.UtcNow,
                    CreatedBy   = currentUser.FullName,
                    UpdatedOn   = null,
                    UpdatedBy   = null,
                    IsActive    = true
                };

                var succeeded = await siteService.AddAsync(newSite);

                if (!succeeded)
                {
                    throw new Exception("Error Saving New Site.");
                }

                var site = await siteService.FindByNameAsync(newSite.Name);

                return(modelConverters.SiteToSiteDtoWithProfiles(site));
            }