public async Task <IActionResult> Handle(EditUserCommand request, CancellationToken cancellationToken)
        {
            var user = await _context.Users.FindAsync(request.UserId);

            if (user == null)
            {
                return(new NotFoundObjectResult(new ErrorModel(ErrorMessages.NotFound, ErrorCodes.NotFound)));
            }

            // make sure no duplicate fullname
            if (request.Body?.FullName != null && !string.Equals(user.FullName, request.Body.FullName, StringComparison.InvariantCultureIgnoreCase))
            {
                if (await _context.Users.AnyAsync(x => x.FullName.ToLower() == request.Body.FullName.ToLower(),
                                                  cancellationToken))
                {
                    return(new ConflictObjectResult(new ErrorModel("Nama sudah digunakan oleh akun lain",
                                                                   ErrorCodes.DataDuplicated)));
                }
            }

            // make sure no duplicate username
            if (request.Body?.Username != null && !string.Equals(user.Username, request.Body.Username, StringComparison.InvariantCultureIgnoreCase))
            {
                if (await _context.Users.AnyAsync(x => x.FullName.ToLower() == request.Body.Username.ToLower(),
                                                  cancellationToken))
                {
                    return(new ConflictObjectResult(new ErrorModel("Username sudah digunakan oleh akun lain",
                                                                   ErrorCodes.DataDuplicated)));
                }
            }

            // calculate shipping cost
            if (request.Body?.Latitude != null &&
                request.Body?.Longitude != null &&
                _bingMaps.IsPointDifferent(user.Latitude,
                                           user.Longitude,
                                           request.Body.Latitude.Value,
                                           request.Body.Longitude.Value))
            {
                var distance = await _bingMaps.CalculateDistance(request.Body.Latitude.Value,
                                                                 request.Body.Longitude.Value, _config.CompanyLatitude, _config.CompanyLongitude, cancellationToken);

                user.ShippingCost = _config.FlatShippingCost * (decimal)distance;
            }

            // project and save the entity
            var updatedEntity = _mapper.Map(request.Body, user);

            _context.Users.Update(updatedEntity);
            await _context.SaveChangesAsync(cancellationToken);

            _logger.LogInformation("Updated user with ID {0}", user.UserId);
            return(new JsonResult(_mapper.Map <UserProfileDto>(user)));
        }
Exemple #2
0
        public async Task <IActionResult> Handle(CalculateShippingCostCommand request,
                                                 CancellationToken cancellationToken)
        {
            _logger.LogDebug("Calculating distance matrix for {0},{1}", request.Latitude, request.Longitude);
            var distance = await _bing.CalculateDistance(request.Latitude, request.Longitude, _config.CompanyLatitude,
                                                         _config.CompanyLongitude, cancellationToken);

            var totalPrice = _config.FlatShippingCost * (decimal)distance;

            _logger.LogInformation("Distance matrix for {0},{1} to company origin is {2} Km", request.Latitude,
                                   request.Longitude, distance);
            return(new JsonResult(new ShippingCostDto(request.Latitude, request.Longitude, distance, totalPrice)));
        }
Exemple #3
0
        public async Task <IActionResult> Handle(EditSupplierCommand request, CancellationToken cancellationToken)
        {
            var supplier = await _context.Suppliers.FindAsync(new object[] { request.SupplierId }, cancellationToken);

            if (supplier == null)
            {
                return(new NotFoundObjectResult(new ErrorModel(ErrorMessages.NotFound, ErrorCodes.NotFound)));
            }

            // make sure no duplicate name
            if (request.Body?.Name != null && !string.Equals(supplier.Name, request.Body.Name, StringComparison.InvariantCultureIgnoreCase))
            {
                if (await _context.Suppliers.AnyAsync(x => x.Name.ToLower() == request.Body.Name.ToLower(),
                                                      cancellationToken))
                {
                    return(new ConflictObjectResult(new ErrorModel("Nama sudah digunakan pada supplier sebelumnya.",
                                                                   ErrorCodes.DataDuplicated)));
                }
            }

            // calculate shipping cost
            if (request.Body?.Latitude != null &&
                request.Body?.Longitude != null &&
                _bingMaps.IsPointDifferent(supplier.Latitude,
                                           supplier.Longitude,
                                           request.Body.Latitude.Value,
                                           request.Body.Longitude.Value))
            {
                var distance = await _bingMaps.CalculateDistance(request.Body.Latitude.Value,
                                                                 request.Body.Longitude.Value, _config.CompanyLatitude, _config.CompanyLongitude, cancellationToken);

                supplier.ShippingCost = _config.FlatShippingCost * (decimal)distance;
            }

            var updatedEntity = _mapper.Map(request.Body, supplier);

            _context.Suppliers.Update(updatedEntity);
            await _context.SaveChangesAsync(cancellationToken);

            _logger.LogInformation("Updated supplier with ID {0}", supplier.SupplierId);
            return(new JsonResult(supplier));
        }
        public async Task <IActionResult> Handle(CreateUserCommand request, CancellationToken cancellationToken)
        {
            if (await _context.Users.AnyAsync(x =>
                                              x.Username.ToLower() == request.Username.ToLower() ||
                                              x.FullName.ToLower() == request.FullName.ToLower(), cancellationToken))
            {
                return(new ConflictObjectResult(new ErrorModel("Username atau nama lengkap sudah ada sebelumnya",
                                                               ErrorCodes.ModelValidation)));
            }

            var user     = _mapper.Map <User>(request);
            var distance = await _bingMaps.CalculateDistance(user.Latitude, user.Longitude, _config.CompanyLatitude,
                                                             _config.CompanyLongitude, cancellationToken);

            user.ShippingCost = _config.FlatShippingCost * (decimal)distance;

            await _context.Users.AddAsync(user, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            _logger.LogInformation("Created user {0} with ID {1}", user.FullName, user.UserId);
            return(new JsonResult(_mapper.Map <UserProfileDto>(user)));
        }
Exemple #5
0
        public async Task <IActionResult> Handle(CreateSupplierCommand request, CancellationToken cancellationToken)
        {
            if (await _context.Suppliers.AnyAsync(x =>
                                                  x.Name.ToLower() == request.Name.ToLower(), cancellationToken))
            {
                return(new ConflictObjectResult(new ErrorModel("Nama supplier sudah ada sebelumnya",
                                                               ErrorCodes.ModelValidation)));
            }

            var supplier = _mapper.Map <Supplier>(request);
            var distance = await _bingMaps.CalculateDistance(supplier.Latitude, supplier.Longitude,
                                                             _config.CompanyLatitude,
                                                             _config.CompanyLongitude, cancellationToken);

            supplier.ShippingCost = _config.FlatShippingCost * (decimal)distance;

            await _context.Suppliers.AddAsync(supplier, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            _logger.LogInformation("Created supplier {0} with ID {1}", supplier.Name, supplier.SupplierId);
            return(new JsonResult(
                       await _context.Suppliers.FindAsync(new object[] { supplier.SupplierId }, cancellationToken)));
        }