public IActionResult Create([FromBody]Vehicle request) { var actionResult = new CustomActionResult { Successful = true, Message = "Vehicle was successfull created!" }; try { request.SetAudit(CurrentLoggedUserId); var vehicle = _vehicleService.Create(request); actionResult.EntityId = vehicle.Id; } catch { actionResult.Successful = false; actionResult.Message = "Create vehicle was unsuccessfully, please try again!"; return Ok(actionResult); } return Ok(actionResult); }
public IActionResult RemoveVendingMachineSchedule([FromRoute] int id, [FromBody] VendingMachineSchedule request) { var scheduleId = request.ScheduleId ?? 0; var schedule = _scheduleService.GetById(scheduleId); var vendingMachineSchedules = schedule.VendingMachineSchedules.ToList(); vendingMachineSchedules.RemoveAll(x => x.ScheduleId == request.ScheduleId && x.VendingMachineId == request.VendingMachineId); schedule.VendingMachineSchedules = vendingMachineSchedules; var actionResult = new CustomActionResult { Successful = true, Message = "Remove Vending Machine from Schedule was successfully!" }; try { schedule.SetAudit(CurrentLoggedUserId); var dbSchedule = _scheduleService.Update(schedule); } catch { actionResult.Successful = false; actionResult.Message = "Remove Vending Machine from Schedule was unsuccessfully, please try again!"; return(Ok(actionResult)); } return(Ok(actionResult)); }
public IActionResult Update([FromRoute] int id, [FromBody] VendingMachineProductPrice request) { var actionResult = new CustomActionResult { Successful = true, Message = "Product price was successfull updated for Vending Machine!" }; try { request.SetAudit(CurrentLoggedUserId); var vendingMachineProductPrice = _vendingMachineProductPriceService.Update(request); actionResult.EntityId = vendingMachineProductPrice.Id; } catch { actionResult.Successful = false; actionResult.Message = "Product price was unsuccessfull updated for Vending Machine, please try again!!"; return(Ok(actionResult)); } return(Ok(actionResult)); }
public IActionResult Create([FromBody] Product request) { var actionResult = new CustomActionResult { Successful = true, Message = "Product was successfull created!" }; try { request.SetAudit(CurrentLoggedUserId); request.PreviousPurchasePrice = request.PurchasePrice; var product = _productService.Create(request); actionResult.EntityId = product.Id; } catch { actionResult.Successful = false; actionResult.Message = "Create product was unsuccessfully, please try again!"; return(Ok(actionResult)); } return(Ok(actionResult)); }
/// <summary> /// Gets the tax amount for a given order /// </summary> /// <param name="order"> /// the order /// </param> /// <returns> the tax amount for a given order</returns> public async Task <CustomActionResult <decimal> > GetTaxForOrder(Models.Models.Order order) { // default result if an error ocurred var result = new CustomActionResult <decimal> { Result = (decimal)0.00, Message = "An Error occurred processing the request.", StatusCode = Common.Web.Enum.StatusCode.Error }; try { string validMessage = string.Empty; var isModelValid = order.IsValid(out validMessage); if (isModelValid) { var taxJarOrder = _mapper.Map <Taxjar.Order>(order); var client = new RestClient(_apiUrl) { Authenticator = new JwtAuthenticator(this._apiKey) }; var request = new RestRequest("taxes") { Method = Method.POST, RequestFormat = DataFormat.Json }; request.AddJsonBody(JsonConvert.SerializeObject(taxJarOrder)); var response = await client.ExecutePostAsync(request); // make sure api call return with 200 if (response.StatusCode != HttpStatusCode.OK) { result.Message = "Tax Jar API did not return success code."; return(result); } var responseData = JsonConvert.DeserializeObject <TaxResponse>(response.Content); result = new CustomActionResult <decimal> { Result = Math.Round((decimal)responseData.Tax.AmountToCollect, this._rounding.All(Char.IsDigit) ? Convert.ToInt32(this._rounding) : 2), StatusCode = Common.Web.Enum.StatusCode.Success, Message = "Success" }; return(result); } else { result.Message = validMessage; return(result); } } catch (Exception ex) { // handle exception here i.e. log into server, app insights , etc return(result); } }
public IActionResult Update([FromRoute] int id, [FromBody] ScheduleCreateOrEtitRequest request) { var actionResult = new CustomActionResult { Successful = true, Message = "Schedule was successfull updated!" }; var scheduleId = request.ScheduleId ?? 0; var schedule = new Schedule(); if (request.ScheduleId.HasValue) { schedule = _scheduleService.GetById(scheduleId); } else { actionResult.Successful = false; actionResult.Message = "Selected Schedule can not be found!"; } foreach (var vendingMachineId in request.VendingMachineIds) { var vendingMachineSchedule = new VendingMachineSchedule { VendingMachineId = vendingMachineId }; vendingMachineSchedule.SetAudit(CurrentLoggedUserId); schedule.VendingMachineSchedules.Add(vendingMachineSchedule); } try { var dbSchedule = _scheduleService.Update(schedule); actionResult.EntityId = dbSchedule.Id; } catch { actionResult.Successful = false; actionResult.Message = "Update schedule was unsuccessfully, please try again!"; return(Ok(actionResult)); } return(Ok(actionResult)); }
public async Task <IActionResult> Login([FromBody] Login request) { var result = await _signInManager.PasswordSignInAsync(request.Email, request.Password, false, false); var loginResult = new CustomActionResult(); if (!result.Succeeded) { loginResult.Successful = false; loginResult.Message = "Invalid username or password!"; return(Ok(loginResult)); } var user = await _signInManager.UserManager.FindByEmailAsync(request.Email); var roles = await _signInManager.UserManager.GetRolesAsync(user); var claims = new List <Claim>(); claims.Add(new Claim(ClaimTypes.Name, request.Email)); foreach (var role in roles) { claims.Add(new Claim(ClaimTypes.Role, role)); } var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JwtSecurityKey"])); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var expiry = DateTime.Now.AddDays(Convert.ToInt32(_configuration["JwtExpiryInDays"])); var token = new JwtSecurityToken( _configuration["JwtIssuer"], _configuration["JwtAudience"], claims, expires: expiry, signingCredentials: creds ); loginResult.Successful = true; loginResult.Token = new JwtSecurityTokenHandler().WriteToken(token); return(Ok(loginResult)); }
public IActionResult Create([FromBody] ScheduleCreateOrEtitRequest request) { var schedule = new Schedule(); schedule.Day = request.Day; schedule.FieldWorkerId = request.FieldWorkerId; schedule.SetAudit(CurrentLoggedUserId); var vendingMachineSchedules = new List <VendingMachineSchedule>(); foreach (var id in request.VendingMachineIds) { var vendingMachineSchedule = new VendingMachineSchedule { VendingMachineId = id }; vendingMachineSchedule.SetAudit(CurrentLoggedUserId); vendingMachineSchedules.Add(vendingMachineSchedule); } schedule.VendingMachineSchedules = vendingMachineSchedules; var actionResult = new CustomActionResult { Successful = true, Message = "Schedule was successfull created!" }; try { var dbSchedule = _scheduleService.Create(schedule); actionResult.EntityId = dbSchedule.Id; } catch { actionResult.Successful = false; actionResult.Message = "Create schedule was unsuccessfully, please try again!"; return(Ok(actionResult)); } return(Ok(actionResult)); }
public IActionResult Update([FromRoute] int id, [FromBody] Product request) { var actionResult = new CustomActionResult { Successful = true, Message = "Update product was successfully!" }; var dbproduct = _productService.GetById(request.Id); if (request.PurchasePrice != dbproduct.PurchasePrice) { request.PreviousPurchasePrice = dbproduct.PurchasePrice; } if (dbproduct.Quantity > request.Quantity) { actionResult.Successful = false; actionResult.Message = $"Quantity can not have value lower than current value in data base! Please enter value larger than {dbproduct.Quantity} "; return(Ok(actionResult)); } try { request.SetAudit(CurrentLoggedUserId); _context.Entry(request).State = EntityState.Deleted; var product = _productService.Update(request); actionResult.EntityId = product.Id; } catch { actionResult.Successful = false; actionResult.Message = "Update product was unsuccessfully, please try again!"; return(Ok(actionResult)); } return(Ok(actionResult)); }
public IActionResult Create([FromBody] Defect request) { var actionResult = new CustomActionResult { Successful = true, Message = "Defect was successfull created!" }; try { request.SetAudit(CurrentLoggedUserId); var defect = _defectService.Create(request); actionResult.EntityId = defect.Id; } catch { actionResult.Successful = false; actionResult.Message = "Create defect was unsuccessfully, please try again!"; return(Ok(actionResult)); } try { var vendingMachine = _vendingMachineService.GetById(request.VendingMachineId); vendingMachine.CostOfDefects += decimal.ToInt32(request.Cost); vendingMachine.NumberOfDefects += 1; _vendingMachineService.Update(vendingMachine); } catch { actionResult.Successful = false; actionResult.Message = "Create defect was successfully, but values for the vending machine was not updated properly, please contact the admin!"; return(Ok(actionResult)); } return(Ok(actionResult)); }
public IActionResult Delete([FromRoute] int id) { var actionResult = new CustomActionResult { Successful = true, Message = "Remove product from vending machine was successfull!" }; try { _vendingMachineProductService.Delete(id); } catch { actionResult.Successful = false; actionResult.Message = "Remove product from vending machine action failed, please try again!"; return(Ok(actionResult)); } return(Ok(actionResult)); }
public IActionResult Create([FromBody] User request) { var appUser = new AppUser { Name = request.Name, MiddleName = request.MiddleName, LastName = request.LastName, Email = request.Email, NormalizedEmail = request.Email.ToUpper(), UserRole = request.Role, AddressId = request.AddressId, VehicleId = request.VehicleId, UserName = request.Email, NormalizedUserName = request.Email.ToUpper() }; var result = _userManager.CreateAsync(appUser, request.Password).Result; var roleResult = _userManager.AddToRoleAsync(appUser, appUser.UserRole.ToString()).Result; var actionResult = new CustomActionResult { Successful = true, Message = "User was successfull created!" }; if (!result.Succeeded) { actionResult.Successful = false; actionResult.Message = "Create user action failed, please try again!"; } if (!roleResult.Succeeded) { actionResult.Successful = false; actionResult.Message += "But Role was not added, please try to update the user!"; } return(Ok(actionResult)); }
public IActionResult Delete([FromBody] AppUser request) { var actionResult = new CustomActionResult { Successful = true, Message = "Delete user was successfull!" }; try { _userService.Delete(request); } catch { actionResult.Successful = false; actionResult.Message = "Delete user action failed, please try again!"; return(Ok(actionResult)); } return(Ok(actionResult)); }
public IActionResult Update([FromRoute] int id, [FromBody] AppUser request) { var actionResult = new CustomActionResult { Successful = true, Message = "Update user was successfull!" }; request.NormalizedEmail = request.Email.Trim().ToUpper(); request.UserName = request.Email.Trim(); request.NormalizedUserName = request.Email.Trim().ToUpper(); var dbUser = _userService.GetById(request.Id); if (dbUser == null) { actionResult.Successful = false; actionResult.Message = "User do not exist!"; return(Ok(actionResult)); } try { request.SetAudit(CurrentLoggedUserId); var user = _userService.Update(request); _signInManager.RefreshSignInAsync(user); actionResult.EntityId = user.Id; } catch { actionResult.Successful = false; actionResult.Message = "Update user action failed, please try again!"; return(Ok(actionResult)); } return(Ok(actionResult)); }
public IActionResult Delete([FromRoute] int id) { var actionResult = new CustomActionResult { Successful = true, Message = "Delete address was successfull!" }; try { _addressService.Delete(id); } catch { actionResult.Successful = false; actionResult.Message = "Delete address action failed, please try again!"; return(Ok(actionResult)); } return(Ok(actionResult)); }
public IActionResult Create([FromBody] StorageImport request) { var actionResult = new CustomActionResult { Successful = true, Message = "Storage Import was successfull created!" }; try { request.SetAudit(CurrentLoggedUserId); var storageImport = _storageImportService.Create(request); actionResult.EntityId = storageImport.Id; } catch { actionResult.Successful = false; actionResult.Message = "Create storage import was unsuccessfully, please try again!"; return(Ok(actionResult)); } try { var product = _productService.GetById(request.ProductId); product.StorageQuantity += request.Quantity; product.SetAudit(CurrentLoggedUserId); _productService.Update(product); } catch { actionResult.Successful = false; actionResult.Message = "Create storage import was successfully, but Storage Quantity of the product was not updated properly, please contact Office worker!"; return(Ok(actionResult)); } return(Ok(actionResult)); }
public IActionResult Create([FromBody] Income request) { var actionResult = new CustomActionResult { Successful = true, Message = "Income was successfully created!" }; try { request.SetAudit(CurrentLoggedUserId); var income = _incomeService.Create(request); actionResult.EntityId = income.Id; } catch { actionResult.Successful = false; actionResult.Message = "Create income was unsuccessfully, please try again!"; return(Ok(actionResult)); } try { var vendingMachine = _vendingMachineService.GetById(request.VendingMachineId); vendingMachine.Income += request.CollectedIncome; _vendingMachineService.Update(vendingMachine); } catch { actionResult.Successful = false; actionResult.Message = "Create income was successfully, but Income value for the vending machine was not updated properly, please contact the admin!"; return(Ok(actionResult)); } return(Ok(actionResult)); }
public async Task <IActionResult> ChangePassword([FromBody] ChangePassword request) { var user = _userService.GetById(CurrentLoggedUserId); var result = await _userManager.ChangePasswordAsync(user, request.CurrentPassword, request.NewPassword); var actionResult = new CustomActionResult { Successful = true, Message = "Password was successfully changed!" }; if (!result.Succeeded) { actionResult.Successful = false; actionResult.Message = "Change Password action failed, please try again!"; return(Ok(actionResult)); } await _signInManager.RefreshSignInAsync(user); return(Ok(actionResult)); }
public IActionResult Update([FromRoute] int id, [FromBody] VendingMachine request) { var actionResult = new CustomActionResult { Successful = true, Message = "Update vending machine informations was successfully!" }; try { request.SetAudit(CurrentLoggedUserId); var vendingMachine = _vendingMachineService.Update(request); actionResult.EntityId = vendingMachine.Id; } catch { actionResult.Successful = false; actionResult.Message = "Update vending machine informations was unsuccessfully, please try again!"; return(Ok(actionResult)); } return(Ok(actionResult)); }
public IActionResult Create([FromBody] Address request) { var actionResult = new CustomActionResult { Successful = true, Message = "Address was successfull created!" }; try { request.SetAudit(CurrentLoggedUserId); var address = _addressService.Create(request); actionResult.EntityId = address.Id; } catch { actionResult.Successful = false; actionResult.Message = "Create address was unsuccessfully, please try again!"; return(Ok(actionResult)); } return(Ok(actionResult)); }
public IActionResult Create([FromBody] VendingMachineProduct request) { var actionResult = new CustomActionResult { Successful = true, Message = "Product was successfull added for Vending Machine!" }; var userId = CurrentLoggedUserId; try { request.SetAudit(userId); var vendingMachineProduct = _vendingMachineProductService.Create(request); } catch { actionResult.Successful = false; actionResult.Message = "Product was unsuccessfull added for Vending Machine, please try again!"; return(Ok(actionResult)); } if (CurrentLoggedUserRole == Role.FieldWorker) { var product = _productService.GetById(request.ProductId); var fieldWorkerProducts = _fieldWorkerProductService.GetByCriteria(new FieldWorkerProductSearchRequest { FieldWorkerId = CurrentLoggedUserId, ProductId = request.ProductId }); var fieldWorkerProduct = fieldWorkerProducts.FirstOrDefault(); try { product.Quantity -= request.Quantity; product.SetAudit(userId); _productService.Update(product); } catch { actionResult.Successful = false; actionResult.Message = "Product was successfully added for Vending Machine, but Quantity for product was not updated, contact your superior!"; return(Ok(actionResult)); } if (fieldWorkerProduct == null) { actionResult.Successful = false; actionResult.Message = "Product was successfully added for Vending Machine, but field worker should not possess these product, contact your superior!"; return(Ok(actionResult)); } else { try { fieldWorkerProduct.Quantity -= request.Quantity; fieldWorkerProduct.SetAudit(userId); _fieldWorkerProductService.Update(fieldWorkerProduct); } catch { actionResult.Successful = false; actionResult.Message = "Product was successfully added for Vending Machine, but Quantity for field worker product was not updated, contact your superior!"; return(Ok(actionResult)); } } } return(Ok(actionResult)); }
/// <summary> /// Gets the tax rate for a given location /// </summary> /// <param name="location"> /// the location /// </param> /// <returns>the tax rate as decimal</returns> public async Task <CustomActionResult <decimal> > GetTaxRateForLocation(Location location) { // default result if an error ocurred var result = new CustomActionResult <decimal> { Result = (decimal)0.00, Message = "An Error occurred processing the request.", StatusCode = Common.Web.Enum.StatusCode.Error }; try { string validMessage = string.Empty; var isModelValid = location.IsValid(out validMessage); if (isModelValid) { var request = new RestRequest($"rates/{location.Zip}") { Method = Method.GET, RequestFormat = DataFormat.Json }; if (!string.IsNullOrEmpty(location.Country)) { request.AddParameter("country", location.Country); } if (!string.IsNullOrEmpty(location.State)) { request.AddParameter("state", location.State); } if (!string.IsNullOrEmpty(location.City)) { request.AddParameter("city", location.City); } if (!string.IsNullOrEmpty(location.Street)) { request.AddParameter("street", location.Street); } var client = new RestClient(this._apiUrl) { Authenticator = new JwtAuthenticator(this._apiKey) }; var response = await client.ExecuteGetAsync(request); // make sure api call return with 200 if (response.StatusCode != HttpStatusCode.OK) { result.Message = "Tax Jar API did not return success code."; return(result); } var responseData = JsonConvert.DeserializeObject <RateResponse>(response.Content); result = new CustomActionResult <decimal> { Result = Math.Round((decimal)responseData.Rate.CombinedRate, this._rounding.All(Char.IsDigit) ? Convert.ToInt32(this._rounding) : 2), StatusCode = Common.Web.Enum.StatusCode.Success, Message = "Success" }; return(result); } else { result.Message = validMessage; return(result); } } catch (Exception ex) { // handle exception here i.e. log into server, app insights , etc return(result); } }
public IActionResult Create([FromBody] StorageExport request) { var actionResult = new CustomActionResult { Successful = true, Message = "Storage Export was successfull created!" }; try { request.SetAudit(CurrentLoggedUserId); var storageExport = _storageExportService.Create(request); actionResult.EntityId = storageExport.Id; } catch { actionResult.Successful = false; actionResult.Message = "Create storage export was unsuccessfully, please try again!"; return(Ok(actionResult)); } try { var product = _productService.GetById(request.ProductId); product.StorageQuantity -= request.Quantity; _productService.Update(product); } catch { actionResult.Successful = false; actionResult.Message = "Create storage export was successfully, but Storage Quantity of the product was not updated properly, please contact Office worker!"; return(Ok(actionResult)); } var fieldWorkerProducts = _fieldWorkerProductService.GetByCriteria(new FieldWorkerProductSearchRequest { FieldWorkerId = request.FieldWorkerId, ProductId = request.ProductId }); try { if (fieldWorkerProducts.Count() == 0) { var fieldWorkerProduct = new FieldWorkerProduct { FieldWorkerId = request.FieldWorkerId, ProductId = request.ProductId, Quantity = request.Quantity }; fieldWorkerProduct.SetAudit(CurrentLoggedUserId); _fieldWorkerProductService.Create(fieldWorkerProduct); } else { var fieldWorkerProduct = fieldWorkerProducts.First(); fieldWorkerProduct.Quantity += request.Quantity; fieldWorkerProduct.SetAudit(CurrentLoggedUserId); _fieldWorkerProductService.Update(fieldWorkerProduct); } } catch { actionResult.Successful = false; actionResult.Message = "Create storage export was successfully, but Field Worker Quantity of the product was not updated properly, please contact Office worker!"; return(Ok(actionResult)); } return(Ok(actionResult)); }