예제 #1
0
        public async Task <int> Insert(BreachedEmailApiModel model)
        {
            var id = _list.Min(i => i.Id);

            model.Id = id + 1;
            _list.Add(model);
            return(id);
        }
예제 #2
0
        public async Task <int> Insert(BreachedEmailApiModel model)
        {
            var sql = "INSERT INTO BreachedEmail (Email) values (@email);SELECT CAST(SCOPE_IDENTITY() as int);";

            return(await _conn.QuerySingleOrDefaultAsync <int>(
                       sql,
                       model));
        }
예제 #3
0
        public async void Post_Ok()
        {
            var model = new BreachedEmailApiModel
            {
                Email = "*****@*****.**"
            };
            var response = await _controller.Post(model);

            Assert.IsType <CreatedResult>(response);
        }
예제 #4
0
        public async void Post_EmailNull_BadRequest()
        {
            _controller.ModelState.AddModelError("Email", "Field is required");
            var model = new BreachedEmailApiModel
            {
                Email = null
            };
            var response = await _controller.Post(model);

            Assert.IsType <BadRequestObjectResult>(response);
        }
예제 #5
0
        public async void Post_Email_Test()
        {
            var postRequest = new HttpRequestMessage(HttpMethod.Post, "/BreachedEmails");


            var model = new BreachedEmailApiModel
            {
                Email = "*****@*****.**"
            };

            postRequest.Content = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");
            var response = await _client.SendAsync(postRequest);

            var responseString = await response.Content.ReadAsStringAsync();

            Assert.Contains("email", responseString);
            Assert.Contains("*****@*****.**", responseString);
        }
예제 #6
0
        public async Task <IActionResult> Post(BreachedEmailApiModel model)
        {
            if (ModelState.IsValid)
            {
                var existing = await _data.Get(model.Email);

                if (existing != null)
                {
                    return(Conflict());
                }

                model.Id = await _data.Insert(model);

                _cache.Set <BreachedEmailApiModel>(model.Email, model);
                return(Created($"{nameof(Post)}/{model.Email}", model));
            }

            return(BadRequest(ModelState));
        }