예제 #1
0
        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();
            }
        }
예제 #2
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");
            }