Exemplo n.º 1
0
        public async Task <IActionResult> Post(AnimalRequestDto animalDto)
        {
            var animal = _mapper.Map <AnimalRequestDto, Animal>(animalDto);
            await _service.AddAnimal(animal);

            var animalresponseDto = _mapper.Map <Animal, AnimalResponseDto>(animal);
            var response          = new ApiResponse <AnimalResponseDto>(animalresponseDto);

            return(Ok(response));
        }
Exemplo n.º 2
0
 public ActionResult <Animal> Post([FromBody] AnimalRequest animalRequest)
 {
     try
     {
         Animal animal = animalService.AddAnimal(animalRequest);
         return(CreatedAtAction(nameof(Get), new { id = animal.ID }, animal));
     } catch (ArgumentException ex)
     {
         return(BadRequest(ex.Message));
     }
 }
Exemplo n.º 3
0
        //public IActionResult Post([FromBody] Animal amigo)
        public IActionResult Post(string nombre, string raza, int edad, string imagen)
        {
            Animal amigo = new Animal()
            {
                Nombre = nombre, Raza = raza, Edad = edad, CentroId = 5, Imagen = imagen
            };
            int result = _animalService.AddAnimal(amigo);

            if (result == 0)
            {
                return(Ok());
            }
            return(BadRequest());
        }
Exemplo n.º 4
0
        public async Task AddAnimal(AnimalObject animal)
        {
            var name = animal.Name;

            if (name is null)
            {
                return;
            }

            Type   type   = Type.GetType($"Zoo.Animals.{animal.Type}");
            Gender gender = (Gender)int.Parse(animal.Gender);

            _animalService.AddAnimal((Animal)Activator.CreateInstance(type, name, gender));

            await Clients.All.Refresh();
        }
 public IActionResult Post([FromBody] AnimalModel animalModel)
 {
     try
     {
         if (_animalService.IsValid(animalModel))
         {
             _animalService.AddAnimal(animalModel);
             return(Ok(animalModel));
         }
         else
         {
             return(BadRequest());
         }
     }
     catch (AppException ex)
     {
         // return error message if there was an exception;
         return(BadRequest(new { message = ex.Message }));
     }
 }
Exemplo n.º 6
0
        private void AddAnimal()
        {
            Console.Write("Enter the name of the animal to add: ");
            var name = Console.ReadLine();

            var currAnimal = _animalService.GetAnimalByName(name);

            if (currAnimal != null)
            {
                ConsoleWrite($"The animal you entered ('{name}') already exists in the file. Please enter a different name.", true);
                return;
            }


            var attrs      = new List <AnimalAttribute>();
            var toContinue = true;

            while (toContinue)
            {
                Console.WriteLine();
                Console.WriteLine($"Select an attribute of animal \"{name}\":");
                Console.WriteLine("[A] Action (verb) e.g. 'fly', 'bark', 'eat cheese'");
                Console.WriteLine("[B] Has e.g. 'four legs', 'wings'");
                Console.WriteLine("[C] Is (adjective) e.g. 'big', 'yellow colour', 'an insect'");
                Console.WriteLine("[X] Cancel");

                var input = GetValidInput(new[] { "A", "B", "C", "X" });

                var message = string.Empty;
                var type    = default(AttributeType);
                switch (input)
                {
                case "A":
                    message = $"A(n) {name} can ";
                    type    = AttributeType.Action;
                    break;

                case "B":
                    message = $"A(n) {name} has ";
                    type    = AttributeType.Has;
                    break;

                case "C":
                    message = $"A(n) {name} is ";
                    type    = AttributeType.Is;
                    break;

                case "X":
                    ConsoleWrite("Cancelled adding animal.", true);
                    return;
                }
                Console.WriteLine();
                Console.Write(message);
                var attr = Console.ReadLine();

                if (string.IsNullOrWhiteSpace(attr))
                {
                    ConsoleWrite("At least one animal attribute must be entered.", true);
                }
                else
                {
                    if (attrs.Exists(
                            a => a.Type == type && a.Text.Equals(attr, StringComparison.OrdinalIgnoreCase)))
                    {
                        ConsoleWrite($"Error: The description you entered already exists for the {name}.", true);
                    }
                    else
                    {
                        attrs.Add(new AnimalAttribute()
                        {
                            Text = attr, Type = type
                        });
                    }
                    Console.WriteLine();
                    Console.Write("Do you want to add another description [Y/N]? ");
                    toContinue = IsYes();
                }
            }

            // save animal to data store (file)
            _animalService.AddAnimal(new Animal()
            {
                Name = name, Attributes = attrs
            });

            // refresh data
            _animals = _animalService.GetAnimals();

            ConsoleWrite($"Done. Successfully added '{name}' to animal list. Data has been refreshed.");
        }
Exemplo n.º 7
0
 public async Task AddAnimal([FromBody] Animal animal)
 {
     await _animalService.AddAnimal(animal);
 }
Exemplo n.º 8
0
 public void AddAnimal([FromBody] Animal animal)
 {
     animalService.AddAnimal(animal);
 }