Exemplo n.º 1
0
        public async Task <ResultModel <string> > AddUpdateLocation(LocationUpdateVM model)
        {
            var specification = new GetLocationByAddressAndDateSpecification(model.Address, model.ProtestDate);
            var location      = await _locationRepo.GetBySpecAsync(specification);

            if (location == null)
            {
                location = new Location()
                {
                    Address     = model.Address,
                    ProtestDate = model.ProtestDate.Date,
                    LastUpdate  = DateTime.Now,
                    PostCount   = 1,
                };

                await _locationRepo.AddAsync(location);
            }
            else
            {
                location.LastUpdate = DateTime.Now;
                location.PostCount += 1;

                await _locationRepo.UpdateAsync(location);
            }

            var result = new ResultModel <string>();

            result.Data = "Saved Succesfully";

            return(result);
        }
        public async Task <IActionResult> PostLocation(LocationUpdateVM model)
        {
            if (!ModelState.IsValid)
            {
                return(ApiResponse(ListModelErrors, codes: ApiResponseCodes.INVALID_REQUEST));
            }
            try
            {
                var data = await _locationService.AddUpdateLocation(model);

                if (data.HasError)
                {
                    return(ApiResponse <object>(errors: data.ErrorMessages.ToArray()));
                }
                return(ApiResponse <object>(message: "Successful", codes: ApiResponseCodes.OK, data: data.Data));
            }
            catch (Exception ex)
            {
                return(HandleError(ex));
            }
        }