Exemple #1
0
        private static void TestPhotoContoller()
        {
            int userId  = AddPhoto("dog.jpg", 3);
            int userId2 = AddPhoto("kitty.jpg", 4);

            // GetAllPhotos
            using (var client = new HttpClient())
            {
                var response = client.GetAsync(UriBasePhotos);

                Console.WriteLine(response.Result.StatusCode);
                string str = response.Result.Content.ReadAsStringAsync().GetAwaiter().GetResult();
                IEnumerable <Photo> photos = JsonConvert.DeserializeObject <IEnumerable <Photo> >(str);
                photos.ToList().ForEach(p => Console.WriteLine(p));
            }

            // DeletePhoto{id}
            using (var client = new HttpClient())
            {
                var response = client.DeleteAsync(UriBasePhotos + userId);

                Console.WriteLine(response.Result.StatusCode);
            }

            // GetPhotoByDate
            using (var client = new HttpClient())
            {
                GetPhotoByDateRequest request = new GetPhotoByDateRequest()
                {
                    Date   = DateTime.ParseExact("11/10/2020", "dd/MM/yyyy", null),
                    UserId = 2
                };

                string jsonString = JsonConvert.SerializeObject(request);
                var    response   = client.PostAsync(
                    UriBasePhotos + "GetPhotoByDate",
                    new StringContent(jsonString, Encoding.UTF8, "application/json"));

                Console.WriteLine(response.Result.StatusCode);
                string str = response.Result.Content.ReadAsStringAsync().GetAwaiter().GetResult();

                var photos = JsonConvert.DeserializeObject <IEnumerable <Photo> >(str);
                photos.ToList().ForEach(p => Console.WriteLine(p));
            }

            // GetPhoto{id}
            using (var client = new HttpClient())
            {
                var response = client.GetAsync(UriBasePhotos + userId2);
                Console.WriteLine(response.Result.StatusCode);
                string str = response.Result.Content.ReadAsStringAsync().GetAwaiter().GetResult();

                var photo = JsonConvert.DeserializeObject <Photo>(str);
                Console.WriteLine(photo);
            }
        }
        public IActionResult GetPhotoByDate([FromBody] GetPhotoByDateRequest request)
        {
            var result = _photoService.GetPhotoByDate(request.Date, request.UserId);

            if (String.IsNullOrEmpty(result.Error))
            {
                return(Ok(result.Photos));
            }
            return(BadRequest(result.Error));
        }