コード例 #1
0
        public DogEntity Add(DogEntity dog)
        {
            _db.Dogs.Add(dog);
            _db.SaveChanges();

            return(dog);
        }
コード例 #2
0
        public bool Update(DogEntity dog)
        {
            _db.Entry(dog).State = EntityState.Modified;

            try
            {
                _db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!DogExists(dog.Id))
                {
                    throw;
                }
                else
                {
                    throw;
                }
            }
            var d = Get(dog.Id);

            if (d is null)
            {
                return(false);
            }

            _db.Dogs.Update(dog);
            _db.SaveChanges();

            return(true);
        }
コード例 #3
0
 public DogEntity UpdateDog(int id, DogEntity dog)
 {
     dog.Id        = id;
     dog.ImageData = AddImage(dog.ImageData);
     _dbContext.Dogs.Update(dog);
     return(dog);
 }
コード例 #4
0
        public DogDto ConvertToDto(
            DogEntity dogEntity,
            IUrlHelper urlHelper,
            ApiVersion apiVersion)
        {
            var dogDto = new DogDto
            {
                Id    = dogEntity.Id,
                Name  = dogEntity.Name,
                Breed = dogEntity.Breed,
            };

            if (dogEntity.ImageData != null)
            {
                dogDto.ImageUrl = urlHelper?.Link(
                    nameof(ImagesController.GetImage),
                    new
                {
                    id        = dogEntity.ImageData.Id,
                    extension = dogEntity.ImageData.Extension,
                    version   = apiVersion.ToString()
                });
            }
            else
            {
                dogDto.ImageUrl = dogEntity.ImageUrl;
            }

            return(dogDto.WithLinks(urlHelper, apiVersion));
        }
コード例 #5
0
        public ActionResult GetDog(int id, ApiVersion apiVersion)
        {
            DogEntity dogEntity = _entityManager.GetDog(id);

            if (dogEntity == null)
            {
                throw new NotFoundException();
            }

            return(Ok(_dogMapper.ConvertToDto(dogEntity, Url, apiVersion)));
        }
コード例 #6
0
        public ActionResult DeleteDog(int id, ApiVersion apiVersion)
        {
            DogEntity dogEntity = _entityManager.GetDog(id);

            if (dogEntity == null)
            {
                throw new NotFoundException();
            }

            _entityManager.DeleteDog(id);
            _entityManager.Save();

            return(NoContent());
        }
コード例 #7
0
 private Dog MapEntityToModel(DogEntity d)
 {
     return(new Dog()
     {
         Name = d.Name,
         BirthDay = d.BirthDay,
         Id = d.Id,
         DogAddress = new Address()
         {
             City = d.DogAddress.City
         },
         CreditCard = d.CreditCard,
         Email = d.Email,
         Vaccines = new List <string>()
     });
 }
コード例 #8
0
        public IActionResult PutDog(
            int id,
            [FromBody] DogDto dogDto,
            ApiVersion apiVersion)
        {
            if (dogDto == null)
            {
                throw new BadRequestException(ErrorMessage.INVALID_REQUEST);
            }

            if (dogDto.ImageData != null && dogDto.ImageUrl != null)
            {
                throw new BadRequestException(ErrorMessage.TOO_MANY_IMAGES);
            }

            DogEntity existingDog = _entityManager.GetDog(id);

            if (existingDog == null)
            {
                throw new NotFoundException();
            }

            try
            {
                existingDog.UpdateFromDto(dogDto);
            }
            catch (System.FormatException)
            {
                throw new BadRequestException(ErrorMessage.INVALID_IMAGE);
            }

            _entityManager.UpdateDog(id, existingDog);
            _entityManager.Save();

            return(CreatedAtRoute(
                       nameof(GetDog),
                       new { id, version = apiVersion.ToString() },
                       _dogMapper.ConvertToDto(existingDog, Url, apiVersion)));
        }
コード例 #9
0
        public void DeleteDog(int id)
        {
            DogEntity dog = GetDog(id);

            _dbContext.Dogs.Remove(dog);
        }
コード例 #10
0
 public DogEntity AddDog(DogEntity dog)
 {
     dog.ImageData = AddImage(dog.ImageData);
     _dbContext.Dogs.Add(dog);
     return(dog);
 }
コード例 #11
0
        public static void Seed(ThinkAboutUsContext db)
        {
            db.Database.EnsureCreated();

            if (!db.Sizes.Any())
            {
                //Ja seed-аме (пополнуваме) Sizes табелата.

                var mini = new SizeEntity()
                {
                    Name = "Mini"
                };
                var small = new SizeEntity()
                {
                    Name = "Small"
                };
                var medium = new SizeEntity()
                {
                    Name = "Medium"
                };
                var large = new SizeEntity()
                {
                    Name = "Large"
                };
                var extraLarge = new SizeEntity()
                {
                    Name = "Extra Large"
                };

                db.Sizes.Add(mini);
                db.Sizes.Add(small);
                db.Sizes.Add(medium);
                db.Sizes.Add(large);
                db.Sizes.Add(extraLarge);

                db.SaveChanges();


                if (!db.Dogs.Any())
                {
                    var sizes = db.Sizes.ToList();

                    var dog0 = new DogEntity()
                    {
                        Status      = Status.Lost,
                        Code        = "000001",
                        Gender      = Gender.Male,
                        Location    = "Skopje, Karpos 4",
                        Breed       = "Golden Retriever",
                        Description = "Very friendly and approachable, like to retrieve tennis balls.",
                        ImageUrl    = "https://proxy.duckduckgo.com/iu/?u=http%3A%2F%2Fwww.hdwallpaper.nu%2Fwp-content%2Fuploads%2F2015%2F03%2FGolden-Retriever-Playing-Among-Flowers.jpg&f=1",
                        Size        = sizes[3]
                    };

                    var dog1 = new DogEntity()
                    {
                        Status      = Status.Homeless,
                        Code        = "500002",
                        Gender      = Gender.Female,
                        Location    = "Veles",
                        Breed       = "Unknown mix",
                        Description = "Very cute pup, is scared at first but after a few minutes becomes very friendly, likes to chew on toys.",
                        ImageUrl    = "https://qtxasset.com/styles/breakpoint_sm_default_480px_w/s3/Luxury%20Travel%20Advisor-1509741245/saddogMartazmataiStockGettyImagesPlusGettyImages.jpg?F7vWla0TyZwwHnSx8PIMZrCbwTNHuIXZ&itok=Y3B6kv3R",
                        Size        = sizes[1],
                    };

                    var dog2 = new DogEntity()
                    {
                        Status      = Status.PendingAdoption,
                        Code        = "007603",
                        Gender      = Gender.Male,
                        Location    = "Bitola",
                        Breed       = "Stray dog",
                        Description = "Brown mixed dog, likes to pet, cuddle and sleep. Is friendly with other dogs and animals",
                        ImageUrl    = "https://proxy.duckduckgo.com/iu/?u=http%3A%2F%2Fst.depositphotos.com%2F2331871%2F4172%2Fi%2F950%2Fdepositphotos_41726543-stock-photo-stray-dog-is-breed-native.jpg&f=1",
                        Size        = sizes[2],
                    };

                    var dog3 = new DogEntity()
                    {
                        Status      = Status.Adopted,
                        Code        = "000642",
                        Gender      = Gender.Male,
                        Location    = "Skopje, Aerodrom",
                        Breed       = "Stray dog",
                        Description = "3 year old white mixed breed looking for a home where it can stay off the cold streets of Skopje.",
                        ImageUrl    = "https://e3.365dm.com/17/08/768x432/230483ef152cde4b60c888bb0e87364022d70d15fa922ce3b8d6c7db405681e1_4080632.jpg?20170823085828",
                        Size        = sizes[3]
                    };

                    var dog4 = new DogEntity()
                    {
                        Status      = Status.Homeless,
                        Code        = "1231666",
                        Gender      = Gender.Male,
                        Location    = "Veles",
                        Breed       = "Shepherd mix",
                        Description = "Big shepherd dog with an even bigger heart, wondering the streets looking for friends and food. Loves to play.",
                        ImageUrl    = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS1JZSLtyJlUV3yEdKxwfKRDhdhUTyMnUB4ZiyUpFVFOrrcNPHb",
                        Size        = sizes[4],
                    };

                    var dog5 = new DogEntity()
                    {
                        Status      = Status.Found,
                        Code        = "1231666",
                        Gender      = Gender.Male,
                        Location    = "Veles",
                        Breed       = "German Shepherd",
                        Description = "Very friendly and approachable, like to retrieve tennis balls.",
                        ImageUrl    = "https://www.royalcanin.in/var/royalcanin/storage/images/subsidiaries/in/home/puppy-and-dog/the-dog/dogs-that-serve-man/rescue-dogs/389164-6-eng-GB/rescue-dogs_articleV3.png",
                        Size        = sizes[4]
                    };

                    db.Dogs.Add(dog0);
                    db.Dogs.Add(dog1);
                    db.Dogs.Add(dog2);
                    db.Dogs.Add(dog3);
                    db.Dogs.Add(dog4);
                    db.Dogs.Add(dog5);

                    db.SaveChanges();
                }

                if (!db.Reports.Any())
                {
                    var dogs = db.Dogs.ToList();

                    var report0 = new ReportEntity
                    {
                        DateReported  = DateTime.Now,
                        ContactNumber = "078-888-999",
                        ContactEmail  = "*****@*****.**",
                        DogId         = dogs[0].Id,
                    };

                    var report1 = new ReportEntity
                    {
                        DateReported  = DateTime.Now,
                        ContactNumber = "078-555-444",
                        ContactEmail  = "*****@*****.**",
                        DogId         = dogs[1].Id,
                    };

                    var report2 = new ReportEntity
                    {
                        DateReported  = DateTime.Now,
                        ContactNumber = "078-222-333",
                        ContactEmail  = "*****@*****.**",
                        DogId         = dogs[2].Id,
                    };

                    var report3 = new ReportEntity
                    {
                        DateReported  = DateTime.Now,
                        ContactNumber = "078-222-333",
                        ContactEmail  = "*****@*****.**",
                        DogId         = dogs[4].Id,
                    };

                    db.Reports.Add(report0);
                    db.Reports.Add(report1);
                    db.Reports.Add(report2);

                    db.SaveChanges();
                }
                ;
            }
        }