示例#1
0
        public static bool CreateKittenUrlDbAndCollection()
        {
            _mongoClient = new MongoClient("mongodb+srv://Steph:[email protected]/");

            //this will create if not exists
            _mongoDatabase = _mongoClient.GetDatabase("KittenUrlDb");

            var collection = _mongoDatabase.GetCollection <KittenUrlDto>("KittenUrlDb");

            //To create the mongodb database and collection you must insert a record.
            var testRecord = new KittenUrlDto()
            {
                LongUrl     = "www.thisisatestlongurl.com/longtestpath",
                ShortUrl    = "www.thisisatesturl.com/testpath",
                DateCreated = DateTime.UtcNow,
            };

            var testSavedRecord = collection.Find(u => u.ShortUrl == testRecord.ShortUrl).ToList();

            //This will create the collection if it does not exist
            if (testSavedRecord.Count == 0)
            {
                collection.InsertOne(testRecord);
            }

            var filter  = new BsonDocument("name", "KittenUrlDb");
            var options = new ListCollectionNamesOptions {
                Filter = filter
            };

            var collectionExists = _mongoDatabase.ListCollectionNames(options).Any();

            return(collectionExists);
        }
示例#2
0
        public void HandleShortenUrl_CallToCreateRecord_Test()
        {
            //arrange
            string       url = "www.testurl.com/testlongurlpath";
            KittenUrlDto dto = new KittenUrlDto();

            _repository.CreateRecord().Returns(dto.Id);
            _repository.UpdateRecord(dto.Id, dto.LongUrl, dto.ShortUrl).Returns(true);
            _repository.testmethod().Returns(true);

            //act
            _repository.HandleShortenUrl(url);
            _repository.testmethod();


            //assert
            //--create record metjod called and id passed in
            //check the kittenurl collection is called
            _repository.Received().HandleShortenUrl("ppp");
            _repository.DidNotReceive().CreateRecord();
            _repository.DidNotReceive().UpdateRecord(dto.Id, dto.LongUrl, dto.ShortUrl);
            _repository.Received().UpdateRecord(dto.Id, dto.LongUrl, dto.ShortUrl);
            _repository.Received().CreateRecord();
            //check a record is returned with an id and empty fields


            Assert.Pass();
        }
示例#3
0
        public async Task <string> CreateUrl(string url)
        {
            KittenUrlDto dto = new KittenUrlDto();
            string       apiResponse;

            using (var httpClient = new HttpClient())
            {
                using (var response = await httpClient.GetAsync("https://localhost:44351/kittenurl/createurl/?url=" + url))
                {
                    apiResponse = await response.Content.ReadAsStringAsync();
                }
            }
            return(apiResponse);
        }
示例#4
0
        public async Task <ObjectId> CreateRecord() //should this be static
        {
            var client   = new MongoClient("mongodb+srv://Steph:[email protected]/sample_geospatial?retryWrites=true&w=majority");
            var database = client.GetDatabase("KittenUrlDb"); //this will create if not exists

            //var dbList = client.ListDatabases().ToList();

            var collection = database.GetCollection <KittenUrlDto>("KittenUrlDb"); //if does not exist it creates

            var model = new KittenUrlDto();
            await collection.InsertOneAsync(model);

            return(model.Id);
        }
示例#5
0
        public async Task <bool> UpdateRecord(ObjectId id, string longUrl, string shortUrl)
        {
            var myShortUrl = new KittenUrlDto()
            {
                Id = id, ShortUrl = shortUrl, LongUrl = longUrl, DateCreated = DateTime.UtcNow
            };

            try
            {
                var replaceOneResult = await _urlCollection.ReplaceOneAsync(
                    doc => doc.Id == myShortUrl.Id,
                    myShortUrl);
            }
            catch (Exception ex)
            {
                ex.ToString();
            }

            return(true);
        }