public async Task <IActionResult> TransactionFinalise(string id, [FromBody] Transactions transactionUpdate) { var dbContext = new sirmotoContext(); var usr = await GetCurrentUserAsync(); var user = await dbContext.AspNetUsers.Where(z => z.Id == usr.Id).ToListAsync(); var transaction = await dbContext.Transactions.Where(z => z.Id == Int32.Parse(id)).ToListAsync(); if (transaction[0] == null) { return(NotFound("Transaction is not found")); } if (transaction[0].UserId != user[0].Id) { return(Forbid("Access Denied")); } if (transaction[0].State == "FINAL") { transaction[0].State = "DONE"; transaction[0].PaymentMethod = transactionUpdate.PaymentMethod; transaction[0].TimeStamp = DateTime.Now; dbContext.Update(transaction[0]); await dbContext.SaveChangesAsync(); ResetEmployees(transaction[0]); return(new ObjectResult(transaction[0])); } return(NotFound("Transaction is not ready")); }
public async Task <IActionResult> FindCompanyForTransaction(string id) { var random = new Random(); var dbContext = new sirmotoContext(); var usr = await GetCurrentUserAsync(); var user = await dbContext.AspNetUsers.Where(z => z.Id == usr.Id).ToListAsync(); if (user[0] == null) { return(NotFound("User not found")); } var transaction = await dbContext.Transactions.Where(z => z.Id == Int32.Parse(id)).ToListAsync(); if (transaction[0].State == "PRODUCT_ADDED") { transaction[0].State = "FIND_COMPANY"; transaction[0].TimeStamp = DateTime.Now; //transaction[0].Company = FindCompany(); dbContext.Transactions.Update(transaction[0]); await dbContext.SaveChangesAsync(); dynamic result = new ExpandoObject(); result.Transaction = transaction[0]; result.Resource = "Transaction/GetTransactionById/" + transaction[0].Id; return(new ObjectResult(JsonConvert.SerializeObject(result))); } return(Forbid()); }
public async Task <IActionResult> CompanyRejectForTransaction(string id) { var dbContext = new sirmotoContext(); var usr = await GetCurrentUserAsync(); var user = await dbContext.AspNetUsers.Where(z => z.Id == usr.Id).ToListAsync(); if (user[0].Companies == null) { return(NotFound("User is a not company")); } var transaction = await dbContext.Transactions.Where(z => z.Id == Int32.Parse(id)).ToListAsync(); if (transaction[0].State == "FIND_COMPANY") { transaction[0].TimeStamp = DateTime.Now; transaction[0].State = "FIND_COMPANY"; transaction[0].Company = await FindCompany(transaction[0], user[0].Companies); dbContext.Transactions.Update(transaction[0]); await dbContext.SaveChangesAsync(); dynamic result = new ExpandoObject(); result.Transaction = transaction[0]; result.Resource = "Transaction/GetTransactionById/" + transaction[0].Id; return(new ObjectResult(JsonConvert.SerializeObject(result))); } return(NotFound("Not yet added a product")); }
public async Task <IActionResult> BeginTransaction([FromBody] Transactions input) { var dbContext = new sirmotoContext(); var usr = await GetCurrentUserAsync(); var user = await dbContext.AspNetUsers.Where(z => z.Id == usr.Id).ToListAsync(); if (user[0] == null) { return(NotFound("User not found")); } var transaction = new Transactions() { Id = GenerateRandomInt(), State = "BEGIN", UserId = user[0].Id }; transaction.Location = input.Location; transaction.TimeStamp = DateTime.Now; transaction.Discount = 0; transaction.CompanyId = 0; transaction.PaymentMethod = "Nothing"; dbContext.Transactions.Add(transaction); await dbContext.SaveChangesAsync(); dynamic result = new ExpandoObject(); result.Transaction = transaction; result.Resource = "Transaction/GetTransactionById/" + transaction.Id; return(new ObjectResult(JsonConvert.SerializeObject(result))); }
public async Task <IActionResult> AddNewSchedule(string id, string data) { var dbContext = new sirmotoContext(); SirmotoDeviceSchedule schedule; try{ schedule = JsonConvert.DeserializeObject <SirmotoDeviceSchedule>(data); } catch (Exception e) { return(BadRequest("Bad json data: " + data)); } if (dbContext.SirmotoDevices.Where(z => z.Id == Int32.Parse(id)).Count() == 0) { return(NotFound("Device not found")); } schedule.Id = GenerateRandomInt(); schedule.DeviceId = Int32.Parse(id); dbContext.SirmotoDeviceSchedule.Add(schedule); await dbContext.SaveChangesAsync(); dynamic result = new ExpandoObject(); result.Schedule = schedule; result.Location = "Device/GetScheduleById/" + id; return(new ObjectResult(result)); }
public async Task <IActionResult> AddProduct(string id, [FromBody] PickedUpProducts[] products) { var randomnum = new Random().Next(); var dbContext = new sirmotoContext(); var usr = await GetCurrentUserAsync(); var user = await dbContext.AspNetUsers.Where(z => z.Id == usr.Id).ToListAsync(); var transaction = await dbContext.Transactions.Where(z => z.Id == Int32.Parse(id)).ToListAsync(); if (transaction.Count == 0) { return(NotFound("Transaction not found")); } else if (transaction[0].UserId != user[0].Id) { return(Forbid()); } else if (transaction[0].State != "BEGIN") { return(Forbid()); } else { foreach (var item in products) { item.Id = GenerateRandomInt(); item.TransactionId = Int32.Parse(id); var product = await dbContext.Products.Where(z => z.Id == item.ProductId).ToListAsync(); if (product[0] == null) { return(BadRequest("Product item not found: " + item.ProductId)); } var getproductType = await dbContext.Products.Where(z => z.Id == item.ProductId).ToListAsync(); var productType = getproductType[0].Type; if (productType == "LAYANAN") { item.Quantity = 1; //item.HitCount++; //dbContext.Update(layanan[0]); } transaction[0].PickedUpProducts.Add(item); } transaction[0].State = "PRODUCT_ADDED"; dbContext.Update(transaction[0]); await dbContext.SaveChangesAsync(); dynamic result = new ExpandoObject(); result.Transaction = transaction; result.Resource = "Transaction/GetTransactionById/" + transaction[0].Id; return(new ObjectResult(JsonConvert.SerializeObject(result))); } }
private async void ResetEmployees(Transactions transactions) { var dbContext = new sirmotoContext(); var list = await dbContext.Employees.Where(w => w.CompanyId == transactions.CompanyId).ToListAsync(); foreach (var item in list) { item.State = "IDLE"; dbContext.Update(item); } await dbContext.SaveChangesAsync(); }
public async Task <IActionResult> RemoveSchedule(string id) { var dbContext = new sirmotoContext(); var schedule = await dbContext.SirmotoDeviceSchedule.Where(z => z.Id == Int32.Parse(id)).ToListAsync(); if (schedule.Count == 0) { return(NotFound("Device not found")); } else { dbContext.SirmotoDeviceSchedule.Remove(schedule[0]); await dbContext.SaveChangesAsync(); } return(new ObjectResult("[INFO] Unregistered schedule no " + id)); }
public async Task <IActionResult> CompanyAcceptForTransaction(string id) { var dbContext = new sirmotoContext(); var usr = await GetCurrentUserAsync(); var user = await dbContext.AspNetUsers.Where(z => z.Id == usr.Id).ToListAsync(); var company = await dbContext.Companies.Where(z => z.UserId == user[0].Id).ToListAsync(); if (company.Count == 0) { return(NotFound("User is a not company")); } var transaction = await dbContext.Transactions.Where(z => z.Id == Int32.Parse(id)).ToListAsync(); if (transaction[0].State == "COMPANY_ADDED") { transaction[0].TimeStamp = DateTime.Now; if (company[0].Employees.Count == 0) { return(BadRequest()); } transaction[0].State = "FINAL"; //var company1 = dbContext.Companies.Where(z => z.UserId == user[0].Id ).ToArray(); var employess = await AssembleEmployees(transaction[0], company[0]); foreach (var item in employess) { transaction[0].PickedUpEmployees.Add(new PickedUpEmployees { Id = GenerateRandomInt(), EmployeeId = item.Id, TransactionId = transaction[0].Id }); } //transaction[0].PickedUpEmployees = ; dbContext.Transactions.Update(transaction[0]); await dbContext.SaveChangesAsync(); dynamic result = new ExpandoObject(); result.Transaction = transaction[0]; result.Resource = "Transaction/GetTransactionById/" + transaction[0].Id; return(new ObjectResult(JsonConvert.SerializeObject(result))); } return(NotFound("Not assigned to a company yet")); }
public async Task <IActionResult> SendSiramCommand(string id) { var dbContext = new sirmotoContext(); var usr = await GetCurrentUserAsync(); var usrId = usr.Id; var device = await dbContext.SirmotoDevices.Where(z => z.Id == Int32.Parse(id)).ToListAsync(); if (device[0] == null) { return(NotFound("[WARN] Device not found")); } else { device[0].DeviceInfo = "STATE_WATERING_ONDEMAND"; dbContext.Update(device[0]); await dbContext.SaveChangesAsync(); } return(new ObjectResult("done")); }
private async Task <Employees[]> AssembleEmployees(Transactions transaction, Companies company) { var dbContext = new sirmotoContext(); var transactions = dbContext.Transactions .Include(t => t.PickedUpProducts); var count = 1; var previousItemQuery = await dbContext.PickedUpProducts.Where(w => w.TransactionId == transaction.Id).ToListAsync(); var previousItem = previousItemQuery[0]; foreach (var item in previousItemQuery) { var product = dbContext.Products.Where(z => z.Id == item.ProductId).ToArray()[0]; if (product.RequiredWorker == 1 && transaction.PickedUpProducts.Count < 4) { continue; } else if (product.RequiredWorker < 3 && transaction.PickedUpProducts.Count < 4) { count += 1; } else if (product.RequiredWorker < 5 && transaction.PickedUpProducts.Count < 5) { count += 2; } previousItem = item; } var employess = dbContext.Employees.Where(z => z.State == "IDLE" && z.CompanyId == company.Id); var employeeslist = await employess.ToArrayAsync(); count = (employess.Count() < count) ? employess.Count() : count; for (int i = 0; i < count; i++) { employeeslist[i].State = "WORKING"; dbContext.Update(employeeslist[i]); } await dbContext.SaveChangesAsync(); return(employess.Take(count).ToArray()); }
public async Task <IActionResult> RegisterDevice([FromBody] SirmotoDevices data) { var random = new Random(); var dbContext = new sirmotoContext(); var usr = await GetCurrentUserAsync(); var user = await dbContext.AspNetUsers.Where(z => z.Email == usr.Email).ToListAsync(); data.Description = "None"; data.Id = random.Next(); data.UserId = usr.Id; data.DeviceInfo = "READY"; data.LastUpdated = DateTime.Now; dbContext.SirmotoDevices.Add(data); await dbContext.SaveChangesAsync(); dynamic result = new ExpandoObject(); result.Sirmoto = data; result.Resource = "Device/GetDeviceById/" + data.Id; return(new ObjectResult(JsonConvert.SerializeObject(result))); }
public async Task <IActionResult> GetCompanyForTransaction(string id) { var random = new Random(); var dbContext = new sirmotoContext(); var usr = await GetCurrentUserAsync(); var user = await dbContext.AspNetUsers.Where(z => z.Id == usr.Id).ToListAsync(); if (user[0] == null) { return(NotFound("User not found")); } var transaction = await dbContext.Transactions.Where(z => z.Id == Int32.Parse(id)).ToListAsync(); var transactionToProcess = transaction[0]; if (transaction[0].State == "FIND_COMPANY") { var company = await FindCompany(transaction[0]); if (company == null) { return(BadRequest("No company is available")); } transaction[0].State = "COMPANY_ADDED"; transaction[0].TimeStamp = DateTime.Now; transaction[0].CompanyId = company.Id; dbContext.Transactions.Update(transaction[0]); await dbContext.SaveChangesAsync(); dynamic result = new ExpandoObject(); result.Transaction = transaction[0]; result.Resource = "Transaction/GetTransactionById/" + transaction[0].Id; return(new ObjectResult(JsonConvert.SerializeObject(result))); } return(NotFound("Not assigned to a company yet")); }
public async Task <IActionResult> Unregister(string id) { var dbContext = new sirmotoContext(); var usr = await GetCurrentUserAsync(); var usrId = usr.Id; var device = await dbContext.SirmotoDevices.Where(z => z.Id == Int32.Parse(id)).ToListAsync(); if (device.Count == 0) { return(NotFound("[WARN] Device not found")); } else if (device[0].UserId != usr.Id) { return(Forbid("[ERRO] Access denied")); } else { dbContext.Remove(device[0]); await dbContext.SaveChangesAsync(); } return(new ObjectResult("[INFO] Unregistered devie no " + id)); }
public async Task <IActionResult> DeleteTrans(string id) { var dbContext = new sirmotoContext(); var usr = await GetCurrentUserAsync(); var usrId = usr.Id; var transactions = await dbContext.Transactions.Where(z => z.Id == Int32.Parse(id)).ToListAsync(); if (transactions[0] == null) { return(NotFound("Transaction not found")); } else if (transactions[0].State != "DONE" || transactions[0].State != "FINAL") { dbContext.Remove(transactions[0]); await dbContext.SaveChangesAsync(); return(new ObjectResult("Cancelled transaction id " + id)); } else { return(Forbid("Transaction cannot be cancelled, id " + id)); } }