public async Task <IActionResult> SignIn([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "sign-in")] HttpRequest req) { try { if (req.Body is null) { throw new NullReferenceException(); } else if (await IsTokenValid(req)) { using User user = await Context.Users.AsNoTracking().FirstOrDefaultAsync(u => u.Email.Equals(req.Query["email"])); if (user is null) { using User newUser = new User { Email = req.Query["email"] }; await IUserRepo.Add(newUser); await Context.SaveChangesAsync(); return(new OkObjectResult(new Response(true, string.Empty, newUser))); } else { return(new OkObjectResult(new Response(true, string.Empty, user))); } } else { return(new UnauthorizedResult()); } } catch (Exception) { throw new Exception("Oops! Something went wrong!"); } }
[FunctionName("Add_Item")] //Create public async Task <IActionResult> AddItem([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "cart/post")] HttpRequest req) { try { if (req.Query is null) { throw new NullReferenceException(); } else if (await IsTokenValid(req)) { string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); using Cart cart = JsonConvert.DeserializeObject <Cart>(requestBody); //check if item has enough stock if (await IsInStock(cart.Id, cart.Quantity)) { using EcoItem ecoItem = await IEcoItemRepo.GetOne(cart.EcoItemId); ecoItem.Quantity -= cart.Quantity; await ICartRepo.Add(cart); IEcoItemRepo.Modify(ecoItem); await Context.SaveChangesAsync(); return(new OkObjectResult(new Response(true, "Item has been added to cart.", cart))); } else { return(new OkObjectResult(new Response(false, "Cannot add to cart, item is out-of-stock.", null))); } } else { return(new UnauthorizedResult()); } } catch (Exception) { throw new Exception("Oops! Something went wrong!"); } }
public async Task <IActionResult> Activate([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "activate")] HttpRequest req) { try { await ISupplyType.Add(new SupplyType { Id = 1, IsActive = true, Name = "Plastic" }); await ISupplyType.Add(new SupplyType { Id = 2, IsActive = true, Name = "Glass" }); await ISupplyType.Add(new SupplyType { Id = 3, IsActive = true, Name = "Paper" }); await ISupplyType.Add(new SupplyType { Id = 4, IsActive = true, Name = "Wood" }); await ISupplyType.Add(new SupplyType { Id = 5, IsActive = true, Name = "Steel" }); await ISupplyType.Add(new SupplyType { Id = 6, IsActive = true, Name = "Rubber" }); await ISupplyType.Add(new SupplyType { Id = 7, IsActive = true, Name = "Can" }); await ISupplyType.Add(new SupplyType { Id = 8, IsActive = true, Name = "Tires" }); await ICraftTypeRepo.Add(new CraftType { Id = 1, IsActive = true, Name = "Bricks" }); await ICraftTypeRepo.Add(new CraftType { Id = 2, IsActive = true, Name = "Curtains" }); await ICraftTypeRepo.Add(new CraftType { Id = 3, IsActive = true, Name = "Christmas Carols" }); await IEcoItemRepo.Add(new EcoItem { Id = 1, IsActive = true, Description = "Eco Bricks", UserId = 1, Quantity = 1, Price = 0.0m, CraftTypeId = 1, IsLookingForItem = true, SupplyTypeId = 0 }); await IEcoItemRepo.Add(new EcoItem { Id = 2, IsActive = true, Description = "Plastic bottle", UserId = 1, Quantity = 1, Price = 0.0m, CraftTypeId = 0, IsLookingForItem = true, SupplyTypeId = 2 }); await IEcoItemRepo.Add(new EcoItem { Id = 3, IsActive = true, Description = "Plastic bottle", UserId = 1, Quantity = 1, Price = 0.0m, CraftTypeId = 1, IsLookingForItem = false, SupplyTypeId = 0 }); await Context.SaveChangesAsync(); return(new OkObjectResult("Cheat activated!")); } catch (Exception ex) { throw new Exception("Oops! Something went wrong!", new Exception(ex.Message)); } }