Ejemplo n.º 1
0
        public ActionResult Create(LocationViewModel viewModel)
        //public ActionResult Create([Required, RegularExpression("[A-Z]")] string name)
        {
            try
            {
                // server-side validation does run automatically, but it doesn't
                //  throw exceptions or short-circuit the mvc pipeline. it just puts errors it sees
                //    into the "ModelState" object (property from the Controller base class)

                // you have to check modelstate yourself
                if (!ModelState.IsValid)
                {
                    // if modelstate contains errors when the view is rendered
                    // they will be put on the page by the div asp-validation-summary and/or span asp-validation-for tag helpers.
                    return(View(viewModel));
                }

                var location = new Location(viewModel.Name, viewModel.Stock);
                _repository.Create(location);

                TempData["CreatedLocation"] = location.Name;

                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                // should log the exception
                ModelState.AddModelError("", "There was a problem creating the location");
                // this error should be more specific if possible, e.g. if i can tell it's because
                // of a duplicate name or something
                return(View(viewModel));
            }
        }
Ejemplo n.º 2
0
        public ActionResult Update(Location location)
        {
            ApiResult <Location> apiResult;

            if (ModelState.IsValid)
            {
                if (location.Id > 0)
                {
                    apiResult = TryExecute(() =>
                    {
                        _locationRepository.Update(location);
                        _unitOfWork.Commit();
                        return(location);
                    }, "Location updated sucessfully");
                }
                else
                {
                    apiResult = TryExecute(() =>
                    {
                        _locationRepository.Create(location);
                        _unitOfWork.Commit();
                        return(location);
                    }, "Location created sucessfully");
                }
            }
            else
            {
                apiResult = ApiResultFromModelErrors <Location>();
            }

            return(Json(apiResult, JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 3
0
        public IHttpActionResult Post([FromBody] Location entity)
        {
            logger.Trace("Call LocationController Post");

            var record = locationRepository.Create(entity);

            return(Created(record));
        }
Ejemplo n.º 4
0
 public IActionResult Add(Location l)
 {
     if (ModelState.IsValid)
     {
         _repository.Create(l);
         return(RedirectToAction("Details", new { id = l.Id }));
     }
     return(View(l));
 }
Ejemplo n.º 5
0
 public ActionResult Create([Bind("Name, Stock")] LocationViewModel location)
 {
     try
     {
         _repository.Create(new Location(location.Name, location.Stock));
         return(RedirectToAction(nameof(Index)));
     }
     catch
     {
         return(View());
     }
 }
Ejemplo n.º 6
0
        public async Task <IActionResult> CreateLocation(LocationCreationDto dto)
        {
            var newLocation = new LocationCreationDto
            {
                Description = dto.Description,
                ProvinceId  = dto.ProvinceId
            };

            await _locationRepository.Create(newLocation);

            return(Ok(dto));
        }
 public IActionResult Create([FromBody] Location Location)
 {
     //  var claims = User.Claims.Select(claim => new { claim.Type, claim.Value }).ToDictionary( t => t.Type, t => t.Value);
     // if(claims.ContainsKey("name")){
     //     if( claims["name"].Equals("ADMIN") || claims["name"].Equals("MANAGER") ){
     Location.Id       = Guid.NewGuid() + "";
     Location.IsDelete = false;
     return(Ok(_repository.Create(Location)));
     //     }
     // }else{
     //     return Forbid();
     // }
     //     return Forbid();
 }
Ejemplo n.º 8
0
 public IActionResult Post([FromBody] Location model)
 {
     if (ModelState.IsValid)
     {
         _repository.Create(model);
         return(Ok(model));
     }
     else
     {
         return(new ResponseResult(Response)
         {
             StatusCode = (int)StatusCodes.Status400BadRequest, ResponseObject = ModelState.ToJson()
         }.ToJsonResult());
     }
 }
Ejemplo n.º 9
0
 public int?CreateLocation(Location location, HttpPostedFileBase locationImage)
 {
     using (TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
     {
         IsolationLevel = IsolationLevel.ReadCommitted,
         Timeout = CommonConfiguration.Configuration.TransactionScope_Timeout
     }))
     {
         var locationId = _locationRepository.Create(location);
         location.AssetLocationId = ProcessIncomingImage(location.ClientId, location.CreateUserId.Value, location.AssetLocationId, null, locationImage);
         if (location.AssetLocationId != null && _locationRepository.UpdateEntity(location) > 0 && location.Id > 0)
         {
             transaction.Complete();
         }
     }
     return(location.Id);
 }
        public async Task <Guid> CreateAsync(CreateLocationDto location)
        {
            var device = await _deviceService.GetByPhoneId(location.DeviceId);

            var newLocation = new Location(
                device,
                double.Parse(location.LocationInfo.PositionX),
                double.Parse(location.LocationInfo.PositionY),
                double.Parse(location.LocationInfo.PositionZ),
                location.LocationInfo.BatteryChargeStatus,
                location.LocationInfo.IsCharging
                );

            await _locationRepository.Create(newLocation);

            return(newLocation.Id);
        }
        public ActionResult Create([Bind("Name")] LocationViewModel viewLocation, IFormCollection collection)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewLocation));
            }

            try
            {
                // give the id as 1 just to get it into the system
                // the create repo does not use that id to form a new DB entry
                var location = new Library.Location(viewLocation.Name, 1);
                _locationRepository.Create(location);
                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                ModelState.AddModelError("", "There was a problem Creating the Location");
                return(View(viewLocation));
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Create new location. The flag IsActual of last saved location is changed to false (if exists).
        /// </summary>
        /// <param name="locationCreateDto"></param>
        /// <returns>Returns new location mapped to locationDto</returns>
        public async Task <LocationDto> CreateLocationAsync(LocationCreateDto locationCreateDto)
        {
            var oldLocation = await locationRepository.GetActualLocationByReservationIdAsync(locationCreateDto.ReservationId);

            if (oldLocation != null)
            {
                oldLocation.IsActual = false;
            }

            Location location = new Location()
            {
                ReservationId = locationCreateDto.ReservationId,
                Latitude      = locationCreateDto.Latitude,
                Longitude     = locationCreateDto.Longitude,
                IsActual      = true,
                DateCreated   = DateTime.Now
            };

            locationRepository.Create(location);
            await locationRepository.SaveChangesAsync();

            return(mapper.Map <LocationDto>(location));
        }
Ejemplo n.º 13
0
        //public IActionResult AddLocation(IFormCollection formData)
        public IActionResult AddLocation(LocationViewModel viewModel)
        {
            // any time you get data input from the user, you need to validate it.

            // if you model bind with a viewmodel with DataAnnotations on it like that,
            // the validations will be automatically checked, and any errors will go into ModelState for you.
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            try
            {
                //var location = new Location(formData["Name"], int.Parse(formData["Stock"]));

                // mapping
                var location = new Location(viewModel.Name, viewModel.Stock);

                // perform whatever operation we need to with the model.
                _locationRepo.Create(location);

                // go back to the list of locations
                // way #1:
                //return View(_locationRepo.GetAll());
                // problem: the URL still will say "/home/addlocation"
                // way #2: tell the browser to redirect to /home/index
                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "error while adding location");
                ModelState.AddModelError("", "there was some error, try again");
                // the asp-validation-summary tag helper (and asp-validation-for) will show any existing model errors
                return(View());
                // the user will see the form again, with an error telling him something went wrong.
            }
        }
Ejemplo n.º 14
0
        public HttpResponseMessage CreateLocation(NewLocationVM vm)
        {
            ApiResult result;

            try
            {
                var model = new LocationModel
                {
                    Name      = vm.Name,
                    FullName  = vm.FullName,
                    Longitude = vm.Longitude,
                    Latitude  = vm.Latitude,
                    TimeZone  = vm.TimeZone
                };

                _locationRepository.Create(model);
                return(Request.CreateResponse(HttpStatusCode.OK,
                                              new ApiResponse
                {
                    Result = ApiResult.Success,
                    JsonContent = JsonConvert.SerializeObject(new { Id = model.Id }, Formatting.None)
                }));
            }
            catch (NotUniqueException)
            {
                result = ApiResult.NotUnique;
            }
            catch (Exception)
            {
                return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
            }

            return(Request.CreateResponse(HttpStatusCode.OK, new ApiResponse {
                Result = result
            }));
        }
 public IActionResult Create([FromBody] Location Location)
 {
     Location.Id = Guid.NewGuid() + "";
     return(Ok(_repository.Create(Location)));
 }
Ejemplo n.º 16
0
 public void Create(Location entity)
 {
     _LocationRepository.Create(entity);
 }