public async Task <IActionResult> UpdateLocation([FromForm] CreateLocationVM location)
        {
            try
            {
                if (location == null)
                {
                    throw new Exception("Location is missing");
                }
                var locationPage = await _locationService.UpdateLocationAsync(location);

                return(Ok(locationPage));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
        public async Task <IActionResult> CreateLocation([FromForm] CreateLocationVM location)
        {
            try
            {
                if (location == null)
                {
                    throw new Exception("Location is missing");
                }
                var locationPage = await _locationService.CreateLocationAsync(location, User.Claims.First(c => c.Type == "UserID").Value);

                return(Ok(locationPage));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
        public async Task <LocationPageVM> UpdateLocationAsync(CreateLocationVM location)
        {
            if (location.Id != null)
            {
                var locationDb = await _db.Locations.GetByIdAsync(location.Id.Value);

                locationDb.Name      = location.Name;
                locationDb.Address   = location.Address;
                locationDb.Longitude = location.Longitude;
                locationDb.Latitude  = location.Latitude;
                locationDb.CompanyId = location.CompanyId;
                locationDb.Currency  = location.Currency;
                var locNew = await _db.Locations.UpdateAsync(locationDb);

                return(new LocationPageVM(locNew));
            }
            throw new Exception("location.Id is missing");
        }
        public async Task <LocationPageVM> CreateLocationAsync(CreateLocationVM location, string userId)
        {
            var newLocation = new Location()
            {
                Name      = location.Name,
                Address   = location.Address,
                Longitude = location.Longitude,
                Latitude  = location.Latitude,
                CompanyId = location.CompanyId,
                Currency  = location.Currency,
                AdminId   = userId
            };
            var dbLocation = await _db.Locations.CreateAsync(newLocation);

            var manager = _db.Managers.GetAll().FirstOrDefault(m => m.UserProfile.UserId == userId);

            if (manager != null)
            {
                manager.LocationId = newLocation.Id;
                await _db.Managers.UpdateAsync(manager);
            }

            return(new LocationPageVM(dbLocation));
        }