public Task Handle(Contracts.ClassifiedAds.V1.Create command)
        {
            var classifiedAd = new ClassifiedAd(new ClassifiedAdId(command.Id),
                                                new UserId(command.OwnerId));

            return(Task.CompletedTask);
        }
Exemple #2
0
        public async Task <ActionResult> Create(ClassifiedAdViewModel model)
        {
            if (ModelState.IsValid)
            {
                var expireDate = model.ExpireDate ?? DateTime.Today.AddDays(30);
                if (model.ExpireTime == null)
                {
                    expireDate = expireDate.AddDays(1);
                }
                var expireTime   = model.ExpireTime ?? DateTime.Today;
                var classifiedAd = new ClassifiedAd()
                {
                    UserId      = User.Identity.GetUserId(),
                    TypeId      = model.TypeId,
                    Title       = model.Title,
                    Description = model.Description,
                    Created     = DateTime.Now,
                    Expires     = expireDate
                                  .AddHours(expireTime.Hour)
                                  .AddMinutes(expireTime.Minute)
                                  .AddSeconds(expireTime.Second)
                };
                _db.ClassifiedAds.Add(classifiedAd);
                await _db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            ViewBag.TypeId = new SelectList(_db.ClassifiedTypes, "Id", "Name");
            return(View(model));
        }
        public IHttpActionResult PutClassifiedAd(int id, ClassifiedAd classifiedAd)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != classifiedAd.ClassifiedAdId)
            {
                return BadRequest();
            }

            db.Entry(classifiedAd).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ClassifiedAdExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
Exemple #4
0
        private void ShowEditClassifiedAdWindow(ClassifiedAd ad)
        {
            var w = new EditClassifiedAdWindow();

            w.DataContext = new EditClassifiedAdViewModel(ad);
            w.ShowDialog();
        }
Exemple #5
0
 public ClassifiedAd_Publish_Spec()
 {
     _classifiedAd = new ClassifiedAd(
         new ClassifiedAdId(Guid.NewGuid()),
         new UserId(Guid.NewGuid())
         );
 }
Exemple #6
0
 public Task Handle(V1.Create command)
 => Handle(
     command.ClassifiedAdId,
     state => ClassifiedAd.Create(
         ClassifiedAdId.FromGuid(command.ClassifiedAdId),
         UserId.FromGuid(command.SellerId)
         )
     );
Exemple #7
0
 public Task Handle(V1.FulfillOrder command)
 => Handle(
     command.ClassifiedAdId,
     state => ClassifiedAd.FulfillOrder(
         state, command.When,
         command.ServiceTypes.Select(PaidService.Find)
         )
     );
        public ActionResult DeleteConfirmed(int id)
        {
            ClassifiedAd classifiedAd = db.ClassifiedAds.Find(id);

            db.ClassifiedAds.Remove(classifiedAd);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        public async Task Handle(object command)
        {
            ClassifiedAd classifiedAd;

            switch (command)
            {
            case ClassifiedAds.V1.Create cmd:
                if (await _store.Exists <ClassifiedAd>(cmd.Id.ToString()))
                {
                    throw new InvalidOperationException($"Entity with id {cmd.Id } already exists");
                }

                classifiedAd = new ClassifiedAd(new ClassifiedAdId(cmd.Id), new UserId(cmd.OwnerId));

                await _store.Save(classifiedAd);

                break;

            case ClassifiedAds.V1.SetTitle cmd:
                classifiedAd = await _store.Load <ClassifiedAd>(cmd.Id.ToString());

                if (classifiedAd == null)
                {
                    throw new InvalidOperationException($"Entity with id {cmd.Id} cannot be found");
                }
                classifiedAd.SetTitle(ClassifiedAdTitle.FromString(cmd.Title));
                await _store.Save(classifiedAd);

                break;

            case ClassifiedAds.V1.UpdateText cmd:
                classifiedAd = await _store.Load <ClassifiedAd>(cmd.Id.ToString());

                if (classifiedAd == null)
                {
                    throw new InvalidOperationException($"Entity with id {cmd.Id} cannot be found");
                }
                classifiedAd.UpdateText(ClassifiedAdText.FromString(cmd.Text));
                await _store.Save(classifiedAd);

                break;

            // Let's not repeat ourselves!

            case ClassifiedAds.V1.UpdatePrice cmd:
                await HandleUpdate(cmd.Id, c => c.UpdatePrice(Price.FromDecimal(cmd.Price, cmd.Currency, _currencyLookup)));

                break;

            case ClassifiedAds.V1.RequestToPublish cmd:
                await HandleUpdate(cmd.Id, c => c.RequestToPublish());

                break;

            default:
                throw new InvalidOperationException($"Command type { command.GetType().FullName } is unknown");
            }
        }
Exemple #10
0
        public void Request_publishing_should_fail_when_title_is_missing()
        {
            var ad = new ClassifiedAd(new ClassifiedAdId(Guid.NewGuid()), new UserId(Guid.NewGuid()));

            ad.UpdateText(ClassifiedAdText.FromString("1981 Talbot. Mint condition, no rust"));
            ad.UpdatePrice(Price.FromDecimal(1500m, "EUR", new FakeCurrencyLookup()));

            Assert.Throws <InvalidEntityStateException>(() => ad.RequestToPublish());
        }
Exemple #11
0
        public void Request_publishing_should_fail_when_text_is_missing()
        {
            var ad = new ClassifiedAd(new ClassifiedAdId(Guid.NewGuid()), new UserId(Guid.NewGuid()));

            ad.SetTitle(ClassifiedAdTitle.FromTextOrHtml("Fine car!"));
            ad.UpdatePrice(Price.FromDecimal(1500m, "EUR", new FakeCurrencyLookup()));

            Assert.Throws <InvalidEntityStateException>(() => ad.RequestToPublish());
        }
Exemple #12
0
        public void Request_publishing_should_fail_when_price_is_missing()
        {
            var ad = new ClassifiedAd(new ClassifiedAdId(Guid.NewGuid()), new UserId(Guid.NewGuid()));

            ad.SetTitle(ClassifiedAdTitle.FromTextOrHtml("Fine car!"));
            ad.UpdateText(ClassifiedAdText.FromString("1981 Talbot. Mint condition, no rust"));

            Assert.Throws <InvalidEntityStateException>(() => ad.RequestToPublish());
        }
        public Task Handle(ClassifiedAds.V1.Create command)
        {
            var ad = ClassifiedAd.Create(
                command.Id,
                command.OwnerId,
                command.CreatedBy,
                command.CreatedAt);

            return(_store.Save(ad));
        }
        private async Task HandleCreate(ClassifiedAds.V1.Create cmd)
        {
            if (await _repository.Exists <ClassifiedAd>(cmd.Id.ToString()))
            {
                throw new InvalidOperationException($"Entity with id {cmd.Id} already exists");
            }

            var classifiedAd = new ClassifiedAd(new ClassifiedAdId(cmd.Id), new UserId(cmd.OwnerId));
            await _repository.Save(classifiedAd);
        }
 public ActionResult Edit([Bind(Include = "ClassifiedAdId,IsPublished,IsExpired,CreatedAt,Title,Description")] ClassifiedAd classifiedAd)
 {
     if (ModelState.IsValid)
     {
         db.Entry(classifiedAd).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(classifiedAd));
 }
        public IHttpActionResult GetClassifiedAd(int id)
        {
            ClassifiedAd classifiedAd = db.ClassifiedAds.Find(id);
            if (classifiedAd == null)
            {
                return NotFound();
            }

            return Ok(classifiedAd);
        }
Exemple #17
0
        private string GetClassifiedText(ClassifiedAd cl)
        {
            var tx = cl.Text.Replace("\n", " ").Replace("\r", " ");

            while (tx.Contains("  "))
            {
                tx = tx.Replace("  ", " ");
            }
            tx = tx.Replace("\"", "\"\"");
            return(tx);
        }
        public IHttpActionResult PostClassifiedAd(ClassifiedAd classifiedAd)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.ClassifiedAds.Add(classifiedAd);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = classifiedAd.ClassifiedAdId }, classifiedAd);
        }
Exemple #19
0
        public void Request_publishing_should_succeed_when_ad_is_complete()
        {
            var ad = new ClassifiedAd(new ClassifiedAdId(Guid.NewGuid()), new UserId(Guid.NewGuid()));

            ad.AddPicture(new PictureId(Guid.NewGuid()), new PictureSize(800, 600), new Uri("http://example.com"));
            ad.SetTitle(ClassifiedAdTitle.FromTextOrHtml("Fine car!"));
            ad.UpdateText(ClassifiedAdText.FromString("1981 Talbot. Mint condition, no rust"));
            ad.UpdatePrice(Price.FromDecimal(1500m, "EUR", new FakeCurrencyLookup()));

            ad.RequestToPublish();
            Assert.Equal(ClassifiedAdState.PendingReview, ad.State);
        }
        public IHttpActionResult DeleteClassifiedAd(int id)
        {
            ClassifiedAd classifiedAd = db.ClassifiedAds.Find(id);
            if (classifiedAd == null)
            {
                return NotFound();
            }

            db.ClassifiedAds.Remove(classifiedAd);
            db.SaveChanges();

            return Ok(classifiedAd);
        }
        // GET: ClassifiedAds/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            ClassifiedAd classifiedAd = db.ClassifiedAds.Find(id);

            if (classifiedAd == null)
            {
                return(HttpNotFound());
            }
            return(View(classifiedAd));
        }
        public async Task Handle(object command)
        {
            switch (command)
            {
            case ClassifiedAds.V1.Create cmd:
                if (await _repository.Exists(cmd.Id.ToString()))
                {
                    throw new InvalidOperationException($"Entity with id {cmd.Id} already exists");
                }

                var classifiedAd = new ClassifiedAd(
                    new ClassifiedAdId(cmd.Id),
                    new UserId(cmd.OwnerId));

                await _repository.Add(classifiedAd);

                await _unitOfWork.Commit();

                break;

            case ClassifiedAds.V1.SetTitle cmd:
                await HandleUpdate(cmd.Id,
                                   c => c.SetTitle(ClassifiedAdTitle.FromString(cmd.Title)));

                break;

            case ClassifiedAds.V1.UpdateText cmd:
                await HandleUpdate(cmd.Id,
                                   c => c.UpdateText(ClassifiedAdText.FromString(cmd.Text)));

                break;

            case ClassifiedAds.V1.UpdatePrice cmd:
                await HandleUpdate(cmd.Id,
                                   c => c.UpdatePrice(Price.FromDecimal(cmd.Price, cmd.Currency, _currencyLookup)));

                break;

            case ClassifiedAds.V1.RequestToPublish cmd:
                await HandleUpdate(cmd.Id,
                                   c => c.RequestToPublish());

                break;

            default:
                throw new InvalidOperationException(
                          $"Command type {command.GetType().FullName} is unknown");
            }
        }
Exemple #23
0
    static void Main()
    {
        // Creates two ads (objects)
        ClassifiedAd first  = new ClassifiedAd();
        ClassifiedAd second = new ClassifiedAd();

        // Number of words for each ad
        first.NumWords = 45;

        second.NumWords = 38;

        // Displays the cost for each add based off the number of words
        Display(first);
        Display(second);
    }
Exemple #24
0
 public void RefreshClassified(bool reloadDb)
 {
     if (reloadDb)
     {
         ClassifiedAd.ReloadAllFromDb();
         ClassifiedView        = CollectionViewSource.GetDefaultView(ClassifiedAd.All);
         ClassifiedView.Filter = ClassifiedFilter;
         RaisePropertyChangedEvent("ClassifiedView");
     }
     else
     {
         ClassifiedView.Refresh();
     }
     RaisePropertyChangedEvent("ClassifiedCount");
 }
Exemple #25
0
    public static void Main()
    {
        var a = new ClassifiedAd
        {
            Category = "Used Cars",
            Words    = 100,
        };
        var b = new ClassifiedAd
        {
            Category = "Help Wanted",
            Words    = 60,
        };

        WriteLine(a);
        WriteLine(b);
    }
Exemple #26
0
    public static void Main()
    {
        ClassifiedAd first  = new ClassifiedAd();
        ClassifiedAd second = new ClassifiedAd();

        first.Category  = "Used cars";
        second.Category = "Help Wanted";
        first.Words     = 100;
        second.Words    = 60;
        double ey  = first.Price;
        double eyy = second.Price;


        WriteLine("The classified ad with {0} words in category {1} costs {2}", first.Words, first.Category, ey.ToString("C"));
        WriteLine("The classified ad with {0} words in category {1} costs {2}", second.Words, second.Category, eyy.ToString("C"));
    }
Exemple #27
0
        public async Task can_save_aggregate()
        {
            var aggregate = new ClassifiedAd();

            aggregate.Apply(AutoFixture.Create <Events.V1.ClassifiedAdCreated>());
            aggregate.Apply(AutoFixture.Create <Events.V1.ClassifiedAdPublished>());
            aggregate.Apply(AutoFixture.Create <Events.V1.ClassifiedAdMarkedAsSold>());

            var sut = new GesAggregateStore((type, id) => id, Connection, Serializer, TypeMapper);

            var result = await sut.Save(aggregate);

            // act & assert

            result.NextExpectedVersion.Should().Be(2);
        }
Exemple #28
0
        public async Task <ActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            ClassifiedAd classifiedAd = await _db.ClassifiedAds.AsNoTracking()
                                        .Include(c => c.Type)
                                        .Include(c => c.User.ContactInfo)
                                        .SingleOrDefaultAsync(c => c.Id == id);

            if (classifiedAd == null)
            {
                return(HttpNotFound());
            }
            return(View(classifiedAd));
        }
        public EditClassifiedAdViewModel(ClassifiedAd ad)
        {
            _ad = ad;

            var lst = new List <int>();

            for (int i = 1; i <= 52; i++)
            {
                lst.Add(i);
            }
            _allIssues = lst;

            lst = new List <int>();
            lst.Add(DateTime.Now.Year - 1);
            lst.Add(DateTime.Now.Year);
            lst.Add(DateTime.Now.Year + 1);
            _allYears = lst;
        }
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            ClassifiedAd classifiedAd = db.ClassifiedAds.Find(id);

            if (classifiedAd == null)
            {
                return(HttpNotFound());
            }
            ClassifiedAdViewModel classifiedAdVM = new ClassifiedAdViewModel();

            classifiedAdVM.ClassifiedAdId = classifiedAd.ClassifiedAdId;
            classifiedAdVM.Title          = classifiedAd.Title;
            classifiedAdVM.Description    = classifiedAd.Description;
            return(View(classifiedAdVM));
        }