Exemplo n.º 1
0
        public async Task <TodoItem> AddTodoItem(Models.TodoItem value, int userId)
        {
            var existingTags = await _context.Tags
                               .Where(t => value.Tags.Contains(t))
                               .ToDictionaryAsync(t => t.Name);

            var item = new TodoItem
            {
                Title       = value.Title,
                Description = value.Description,
                OwnerId     = userId,

                Completed = null,
                Created   = DateTime.Now,
            };

            await UpdateTodoTags(item, value.Tags);

            _context.Add(item);

            await _context.SaveChangesAsync();

            StopTracking(item);

            return(item);
        }
Exemplo n.º 2
0
        public async Task <Collection> AddAsync(Collection collection)
        {
            var value = _database.Add(collection);
            await _database.SaveChangesAsync();

            value.State = EntityState.Detached;
            return(value.Entity);
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Post([FromBody] TodoBag value)
        {
            var todo = Todo.CreateFrom(value);

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

            return(CreatedAtAction("Get", new { id = todo.Id }, todo));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Post([FromBody] CreateTodoRecord record, CancellationToken cancellationToken)
        {
            var todoRecord = TodoRecordBuilder.New.WithTitle(record.Title).Build();

            _dbContext.Add(todoRecord);
            await _dbContext.SaveChangesAsync(cancellationToken);

            return(CreatedAtAction(nameof(GetById), new { id = todoRecord.Id }, todoRecord));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Create([Bind("TodoTaskId,Name,IsComplete")] TodoTask todoTask)
        {
            if (ModelState.IsValid)
            {
                ctx.Add(todoTask);
                await ctx.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(todoTask));
        }
Exemplo n.º 6
0
        public async Task <bool> AddItemAsync(Item item)
        {
            int count = 0;

            using (var context = new TodoDbContext())
            {
                context.Add(item);
                count = await context.SaveChangesAsync();
            }
            return(await Task.FromResult(count == 1));
        }
Exemplo n.º 7
0
        public async Task <IActionResult> Create([Bind("ID,Text,IsComplete")] TodoItem todoItem)
        {
            if (ModelState.IsValid)
            {
                _context.Add(todoItem);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(todoItem));
        }
Exemplo n.º 8
0
        public async Task <TodoItem> AddNewTodoItem(string description)
        {
            var todoItem = new TodoItem {
                Description = description
            };

            _context.Add(todoItem);

            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(todoItem);
        }
Exemplo n.º 9
0
        public async Task CreateNewTodoItem(string todoItemText)
        {
            if (string.IsNullOrWhiteSpace(todoItemText))
            {
                throw new ArgumentException("value cannot be empty", nameof(todoItemText));
            }

            var newTodo = TodoItem.NewTodo(todoItemText);

            _context.Add(newTodo);

            await _context.SaveChangesAsync();
        }
Exemplo n.º 10
0
        public async Task <IActionResult> Create([Bind("Id,Text,DateDue")] TodoItem todoItem)
        {
            if (ModelState.IsValid)
            {
                User activeUser = await _context.Users.FirstOrDefaultAsync(u => u.isActive);

                todoItem.Id          = Guid.NewGuid();
                todoItem.DateCreated = DateTime.Now.Date;
                todoItem.UserId      = activeUser.userID;
                _context.Add(todoItem);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(todoItem));
        }
Exemplo n.º 11
0
        public async void AddItemToDatabase_ItemCountOne_ReturnOneItem()
        {
            //SQL Lite
            var connectionStringBuilder =
                new SqliteConnectionStringBuilder {
                DataSource = ":memory:"
            };
            var connection = new SqliteConnection(connectionStringBuilder.ToString());


            var options = new DbContextOptionsBuilder <TodoDbContext>()
                          .UseSqlite(connection)
                          .UseLoggerFactory(new LoggerFactory(
                                                new[] { new LogToActionLoggerProvider((log) =>
                {
                    _output.WriteLine(log);
                }) }))
                          .Options;

            //Arrange
            using (var context = new TodoDbContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();
                var item = new quickstartcore31.Models.Item();
                item.Completed   = false;
                item.Description = "Todo 123";
                item.Name        = "Todo";
                item.Id          = Guid.NewGuid().ToString();

                context.Add(item);

                context.SaveChanges();
            }
            //act
            using (var context = new TodoDbContext(options))
            {
                var inMemoryRepository = new CosmoDBEFRepository <Models.Item>(context);
                Expression <Func <Models.Item, bool> > all = (i) => true;
                var fetchedItem = await inMemoryRepository.GetItemsAsync(all);

                //Assert
                Assert.Single(fetchedItem);
            }
            //Assert.Equal(EntityState.Added, context.Entry(item).State);
        }
Exemplo n.º 12
0
        public async Task <IActionResult> AddEmployee()
        {
            var role = await _todoDbContext.Roles.SingleOrDefaultAsync(x => x.Name == "Admin");

            var emp = new Employee {
                Email    = "*****@*****.**",
                Name     = "Филипп",
                Surname  = "Хамицевич",
                Position = "Программист",
                Nickname = "homa_inc",
                Password = "******",
                RoleId   = role.RoleId,
                Role     = role
            };

            _todoDbContext.Add(emp);
            await _todoDbContext.SaveChangesAsync();

            return(new ObjectResult(emp));
        }
Exemplo n.º 13
0
        public async void AddItemToDatabase_ItemCountOne_ReturnOneItem()
        {
            var options = new DbContextOptionsBuilder <TodoDbContext>()
                          .UseCosmos(
                accountEndpoint: "https://localhost:8081",
                accountKey: "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
                databaseName: "TodoList")
                          .UseLoggerFactory(new LoggerFactory(
                                                new[] { new LogToActionLoggerProvider((log) =>
                {
                    _output.WriteLine(log);
                }) }))
                          .Options;

            //Arrange
            using (var context = new TodoDbContext(options))
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();
                var item = new quickstartcore31.Models.Item();
                item.Completed   = false;
                item.Description = "Todo 123";
                item.Name        = "Todo";
                item.Id          = Guid.NewGuid().ToString();
                context.Add(item);
                context.SaveChanges();
            }
            //act
            using (var context = new TodoDbContext(options))
            {
                var inMemoryRepository = new CosmoDBEFRepository <Models.Item>(context);
                Expression <Func <Models.Item, bool> > all = (i) => true;
                var fetchedItem = await inMemoryRepository.GetItemsAsync(all);

                //Assert
                Assert.Single(fetchedItem);
            }
            //Assert.Equal(EntityState.Added, context.Entry(item).State);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Creates a item.
        /// Returns null if user is not found.
        /// </summary>
        /// <param name="userId">User id</param>
        /// <param name="createDto">todo item to be created</param>
        /// <returns>Todo item</returns>
        public TodoItem CreateItem(int userId, CreateTodoItemDto createDto)
        {
            var user = dbContext.Users.FirstOrDefault(u => u.UserId == userId);

            if (user == null)
            {
                return(null);
            }

            var toBeCreated = new TodoItem
            {
                Description  = createDto.Description,
                user         = user,
                LastModified = DateTime.Now
            };

            var result = dbContext.Add(toBeCreated).Entity;

            dbContext.SaveChanges();

            return(result);
        }
Exemplo n.º 15
0
        public async Task <IActionResult> PostTaskAsync([FromBody] PostTaskRequest request)
        {
            Logger?.LogDebug("'{0}' has been invoked", nameof(PostTaskAsync));

            var response = new SingleResponse <Task>();

            try
            {
                var existingEntity = await DbContext.GetTaskByNameAsync(new Task { Name = request.Name });

                if (existingEntity != null)
                {
                    ModelState.AddModelError("TaskName", "Task name already exists");
                }

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

                var entity = request.ToEntity();

                DbContext.Add(entity);

                await DbContext.SaveChangesAsync();

                response.Model = entity;
            }
            catch (Exception ex)
            {
                response.DidError     = true;
                response.ErrorMessage = "There was an internal error, please contact to technical support";

                Logger?.LogCritical("There was an error on '{0}' invocation: {1}", nameof(PostTaskAsync), ex);
            }
            return(response.ToHttpResponse());
        }
Exemplo n.º 16
0
        public IEnumerable <WeatherForecast> Get()
        {
            var rng = new Random();

            var todo = new Todo()
            {
                Name = rng.Next().ToString()
            };

            using (MiniProfiler.Current.Step("InitUser"))
            {
                _todoDbContext.Add(todo);

                _todoDbContext.SaveChanges();
            }

            return(Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
                   .ToArray());
        }
Exemplo n.º 17
0
 public void Create(Assignee element)
 {
     assigneeContext.Add(element);
     assigneeContext.SaveChanges();
 }
Exemplo n.º 18
0
 public async void CreateTodo(Todo todo)
 {
     _context.Add(todo);
     await _context.SaveChangesAsync();
 }
Exemplo n.º 19
0
 public void Create(Todo element)
 {
     toDoContext.Add(element);
     toDoContext.SaveChanges();
 }
Exemplo n.º 20
0
 public void Create(T entity)
 {
     Context.Add(entity);
     Save();
 }