Пример #1
0
        // POST: api/Todo
        public async Task<HttpResponseMessage> Post(TodoItem todoItem)
        {
            if( todoItem == null)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, "No data was posted");
            }

            return await SaveTodoAndGetResponse(todoItem);
        }
Пример #2
0
        public static MongoTodoItem Map(TodoItem item)
        {
            var mongoItem = new MongoTodoItem()
            {
                Id = item.Id,
                Description = item.Text,
                Done = item.Done
            };

            return mongoItem;
        }
Пример #3
0
        public static TodoItem Map(MongoTodoItem mongoItem)
        {
            var item = new TodoItem()
            {
                Id = mongoItem.Id,
                Text = mongoItem.Description,
                Done = mongoItem.Done
            };

            return item;
        }
Пример #4
0
        public void Map_HasValidTodoItem_MapsToCorrectMongoTodoItem()
        {
            var item = new TodoItem()
            {
                Id = "MySuperAwesomeId2",
                Text = "Pick up the kids in the kindergarten after work",
                Done = true
            };

            var mongoItem = TodoMapper.Map(item);
            Assert.NotNull(mongoItem);
            Assert.Equal(item.Done, mongoItem.Done);
            Assert.Equal(item.Id, mongoItem.Id);
            Assert.Equal(item.Text, mongoItem.Description);
        }
Пример #5
0
        private async Task<HttpResponseMessage> SaveTodoAndGetResponse(TodoItem item)
        {
            var exists = await _repo.Exists(item.Text);
            if (item.Id == null && exists == false)
            {

                var mongoItem = new MongoTodoItem()
                {
                    Done = item.Done,
                    Description = item.Text
                };


                await _repo.Save(item);

                return Request.CreateResponse<string>(HttpStatusCode.Created, "Todo created");
            }
            else
            {
                return Request.CreateResponse<string>(HttpStatusCode.Conflict, "Item allready exists in database");
            }

        }
Пример #6
0
        public async Task<TodoItem> Save(TodoItem item)
        {
            var collection = GetCollection();

            if(item != null && item.Id == null)
            {
                var mongoItem = TodoMapper.Map(item);
                await collection.InsertOneAsync(mongoItem, CancellationToken.None);
                return TodoMapper.Map(mongoItem);
            }

            else if (item != null)
            {
                //TODO: UPdate this to not overwrite the hash/salt
                var mongoItem = TodoMapper.Map(item);

                if(await ExistsByText(item.Text))
                {
                    await collection.FindOneAndReplaceAsync(x => x.Description == item.Text, mongoItem);

                }
                else if(await Exists(item.Id))
                {
                    await collection.FindOneAndReplaceAsync(x => x.Id == item.Id, mongoItem);
                }
                return TodoMapper.Map(mongoItem);
            }
            return item;
        }