Esempio n. 1
0
        public async Task <ActionResult <User> > PostUser(User user)
        {
            var takenName = await _context.Users.Where(u => u.UserName == user.UserName).FirstOrDefaultAsync();

            // if the name already exists and is unused, use it
            if (!(takenName is null))
            {
                if (!takenName.IsActive)
                {
                    // Currently the users are stuck as active until the server restarts.
                    // TODO implement logout and timeout system
                    takenName.IsActive = true;
                    await _context.SaveChangesAsync();

                    return(takenName);
                }
                // this attaches a number to the user's name to avoid duplicates
                int i = 1;
                while (await _context.Users.Where(u => u.UserName == user.UserName + $" ({i})").AnyAsync())
                {
                    i++;
                }

                user.UserName += $" ({i})";
            }

            _context.Users.Add(user);
            await _context.SaveChangesAsync();


            return(CreatedAtAction(nameof(GetUser), new { id = user.Id }, user));
        }
Esempio n. 2
0
        public async Task <IActionResult> PutBoard([FromRoute] long id, [FromBody] Board board)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != board.board_id)
            {
                return(BadRequest());
            }

            _context.Entry(board).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BoardExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutTodoItem(long id, TodoItem todoItem)
        {
            if (id != todoItem.TodoId)
            {
                return(BadRequest());
            }

            _context.Entry(todoItem).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TodoItemExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Esempio n. 4
0
        public async Task <IActionResult> Create([Bind("Id,Title,CreatedAt,Text,Tags")] Board board)
        {
            if (ModelState.IsValid)
            {
                _context.Add(board);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(board));
        }
Esempio n. 5
0
        public async Task <RepairCase> Add(RepairCase entity)
        {
            entity.RegistrationTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss");
//            await _context.RepairCases
//                .Include(x => x.Worker)
//                .Include(x => x.Equipment)
//                .FirstAsync();
            await _context.RepairCases.AddAsync(entity);

            await _context.SaveChangesAsync();

            return(await GetById(entity.Id));
        }
Esempio n. 6
0
        public async Task CreateBoard(Board board, bool randomBoat = true)
        {
            _context.Boards.Add(board);
            await _context.SaveChangesAsync();

            List <Cellule> cellules = new List <Cellule>();

            var cellCount = 0;

            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    Cellule cell = new Cellule(_celluleController.GetUniqueId() + cellCount, board.Id, j, i, false, false);
                    cellules.Add(cell);
                    cellCount++;
                }
            }

            if (randomBoat)
            {
                new BoardGeneratorControllerV2().SetRandomBoat(cellules);
            }

            foreach (Cellule cell in cellules)
            {
                await _celluleController.CreateCellule(cell);
            }
        }
Esempio n. 7
0
        public async Task <ActionResult <Message> > PostMessage(Message message)
        {
            _context.Messages.Add(message);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetMessage", new { id = message.Id }, message));
        }
Esempio n. 8
0
        public async Task <ApplicationUser> Add(ApplicationUser entity)
        {
            await _context.ApplicationUsers.AddAsync(entity);

            await _context.SaveChangesAsync();

            return(await GetById(entity.Id));
        }
Esempio n. 9
0
        public async Task <Owner> Add(Owner entity)
        {
            await _context.Owners.AddAsync(entity);

            await _context.SaveChangesAsync();

            return(await GetById(entity.Id));
        }
Esempio n. 10
0
            public async Task <int> Handle(CreateGameCommand request, CancellationToken cancellationToken)
            {
                var game = await _context.Games
                           .AddAsync(new Game(request), cancellationToken);

                await _context.SaveChangesAsync(cancellationToken);

                return(game.Entity.Id);
            }
Esempio n. 11
0
            public async Task <int> Handle(DeleteGameCommand request, CancellationToken cancellationToken)
            {
                Game game = await _context.Games.GetByIdWithVisits(request.Id, cancellationToken);

                Throw.IsNull(game, ExceptionTexts.NoGameWithGivenId);
                _context.Visits.RemoveRange(game.Visits);
                _context.Games.Remove(game);
                await _context.SaveChangesAsync(cancellationToken);

                return(game.Id);
            }
Esempio n. 12
0
            public async Task <int> Handle(EditGameCommand request, CancellationToken cancellationToken)
            {
                Game game = await _context.Games.GetById(request.Id, cancellationToken);

                Throw.IsNull(game, ExceptionTexts.NoGameWithGivenId);

                game.Update(request);
                _context.Games.Update(game);
                await _context.SaveChangesAsync(cancellationToken);

                return(game.Id);
            }
Esempio n. 13
0
        public async Task <Equipment> Add(Equipment entity)
        {
            if (entity.Owner != null)
            {
                await _context.Equipments
                .Include(x => x.Owner)
                .FirstAsync();
            }

            await _context.Equipments.AddAsync(entity);

            await _context.SaveChangesAsync();

            return(await GetById(entity.Id));
        }
Esempio n. 14
0
            public async Task <DetailsViewModel> Handle(GameDetailQuery request, CancellationToken cancellationToken)
            {
                Game game = await _context.Games
                            .Include(x => x.Visits)
                            .Where(x => x.Id == request.Id)
                            .FirstOrDefaultAsync(cancellationToken);

                Throw.IsNull(game, ExceptionTexts.NoGameWithGivenId);

                AddVisitToGame(game, request.IsFromWeb);
                await _context.SaveChangesAsync(cancellationToken);

                var model = new DetailsViewModel(game);

                return(model);
            }
Esempio n. 15
0
 //POST assignment
 public async Task CreateNewAssignment(Assignment assignment)
 {
     assignment.Date = DateTime.Now;
     _context.Assignments.Add(assignment);
     await _context.SaveChangesAsync();
 }