public UserAdvertiseDTO(UserAdvertise userAdvertise)
 {
     Root = new Root
     {
         IsNegotiate   = userAdvertise.IsNegotiate,
         IsOnWarranty  = userAdvertise.IsOnWarranty,
         PaymentOption = userAdvertise.PaymentOption,
         Status        = userAdvertise.Status,
         Category      = userAdvertise.Category,
         Photos        = userAdvertise.AdvertisePhotos,
         AdvertiseDTO  = new AdvertiseDTO
         {
             Id               = userAdvertise.Advertise.Id,
             UniqueId         = userAdvertise.Advertise.UniqueId,
             Title            = userAdvertise.Advertise.Title,
             District         = userAdvertise.Advertise.District,
             City             = userAdvertise.Advertise.City,
             Price            = userAdvertise.Advertise.Price,
             PublishedAt      = userAdvertise.Advertise.PublishedAt,
             AdvertiseInfoDTO = new AdvertiseInfoDTO
             {
                 Quantity    = userAdvertise.Advertise.AdvertiseInfo.Quantity,
                 Hint        = userAdvertise.Advertise.AdvertiseInfo.Hint,
                 Color       = userAdvertise.Advertise.AdvertiseInfo.Color,
                 Description = userAdvertise.Advertise.AdvertiseInfo.Description
             }
         },
         User = new AdvertiseUser {
             FirstName = userAdvertise.AppUser.FirstName,
             LastName  = userAdvertise.AppUser.LastName,
             UserName  = userAdvertise.AppUser.UserName,
         }
     };
 }
 public LoadHomeAdvertiseDTO(UserAdvertise userAdvertise)
 {
     HomeAdvertises = new HomeAdvertiseDTO
     {
         UniqueId         = userAdvertise.Advertise.UniqueId,
         Title            = userAdvertise.Advertise.Title,
         District         = userAdvertise.Advertise.District,
         City             = userAdvertise.Advertise.City,
         Price            = userAdvertise.Advertise.Price,
         AdvertiseInfoDTO = new HomeAdvertiseInfoDTO
         {
             Hint = userAdvertise.Advertise.AdvertiseInfo.Hint,
         },
         User = new AdvertiseUser {
             FirstName = userAdvertise.AppUser.FirstName,
             LastName  = userAdvertise.AppUser.LastName,
         }
     };
 }
        public async static Task AddData(DataContext dataContext, UserManager <AppUser> userManager, RoleManager <Role> roleManager)
        {
            if (!roleManager.Roles.Any())
            {
                var roles = new List <Role>
                {
                    new Role {
                        Name = "Admin"
                    },
                    new Role {
                        Name = "Moderator"
                    },
                    new Role {
                        Name = "Seller"
                    },
                    new Role {
                        Name = "Normal"
                    },
                };
                foreach (var role in roles)
                {
                    await roleManager.CreateAsync(role);
                }
            }

            if (!userManager.Users.Any())
            {
                var user = new AppUser
                {
                    UserName  = "******",
                    Email     = "*****@*****.**",
                    FirstName = "Test",
                    LastName  = "Test",
                };

                await userManager.CreateAsync(user, "password");

                await userManager.AddToRoleAsync(user, "Normal");
            }


            if (!dataContext.UserAdvertise.Any())
            {
                var userAd = new UserAdvertise
                {
                    Advertise = new Advertise
                    {
                        Title         = "Dell laptop with 16GB ram",
                        City          = "Cairo",
                        District      = "Somewhere",
                        PublishedAt   = DateTime.UtcNow,
                        Price         = 20.50,
                        AdvertiseInfo = new AdvertiseInfo
                        {
                            Color       = "Black",
                            Description = "Very long description",
                            Hint        = "short info",
                            Quantity    = 2
                        },
                    },
                    AppUser       = await dataContext.Users.FirstOrDefaultAsync(x => x.UserName == "test"),
                    Status        = Status.Pending,
                    IsNegotiate   = true,
                    IsOnWarranty  = true,
                    PaymentOption = PaymentOption.Cash,
                };

                userAd.Advertise.UniqueId = AdvertiseUniqueId.NewId(userAd.Advertise.Title);
                await dataContext.UserAdvertise.AddAsync(userAd);

                await dataContext.SaveChangesAsync();
            }
        }
Beispiel #4
0
            public async Task <UserAdvertiseDTO> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await dataContext.Users.FirstOrDefaultAsync(x => x.Id == currentUser.UserId);

                if (user is null)
                {
                    throw new HttpContextException(HttpStatusCode.NotFound, new { User = "******" });
                }


                var advertise = new Advertise
                {
                    Title         = request.Title,
                    District      = request.District,
                    City          = request.City,
                    Price         = request.Price,
                    PublishedAt   = DateTime.UtcNow,
                    AdvertiseInfo = new AdvertiseInfo
                    {
                        Color       = request.AdvertiseInfo.Color,
                        Description = request.AdvertiseInfo.Description,
                        Hint        = request.AdvertiseInfo.Hint,
                        Quantity    = request.AdvertiseInfo.Quantity,
                    },
                };

                advertise.UniqueId = AdvertiseUniqueId.NewId(advertise.Title);
                var photos = new List <Photo>();

                if (request.Photos.Count > 0)
                {
                    foreach (var photo in request.Photos)
                    {
                        var result = await cloudinary.UploadPhoto(photo);

                        photos.Add(new Photo
                        {
                            Id  = result.Id,
                            Url = result.Url
                        });
                    }
                }
                var userAdvertise = new UserAdvertise
                {
                    Category        = request.Category,
                    Status          = Status.Pending,
                    IsNegotiate     = request.IsNegotiate,
                    IsOnWarranty    = request.IsOnWarranty,
                    PaymentOption   = request.PaymentOption,
                    Advertise       = advertise,
                    AppUser         = user,
                    AdvertisePhotos = photos
                };

                await dataContext.UserAdvertise.AddAsync(userAdvertise);

                var success = await dataContext.SaveChangesAsync() > 0;

                if (success)
                {
                    return(new UserAdvertiseDTO(userAdvertise));
                }
                throw new Exception("Server Error - creating new add");
            }