Exemplo n.º 1
0
        private async Task HandleAsync(AddOwnerEvent @event)
        {
            var rentalProperty = _context.RentalProperty.FirstOrDefault(p => p.OriginalId == @event.PropertyId);

            if (rentalProperty != null)
            {
                var address = new OwnerAddress(@event.StreetNumber, @event.City, @event.StateProv, @event.Country, @event.ZipPostCode);

                var owner = new RentalPropertyOwner(@event.OriginalId, @event.FirstName, @event.LastName, @event.ContactEmail,
                                                    @event.ContactTelephone1, @event.ContactTelephone2, address, rentalProperty.Id, DateTime.Now, DateTime.Now);

                _context.Add(owner);

                try
                {
                    await _context.SaveChangesAsync();

                    Log.Information("Owner {Owner} has been added to property {Property} successfully", @event.FirstName + " " + @event.LastName, rentalProperty.PropertyName);

                    // Send message so that lease service can consume to queue app_approved


                    // need to make sure the rental property exists in Lease service then send message if it does.
                    //************************************************
                    // It may not need this because as ApproveApplicaiton Event will send property including owner so that it should be created in Lease service already
                    //************************************************

                    /*
                     * AddOwnerEvent e = new AddOwnerEvent(new Guid(), @event.PropertyId, @event.UserName, @event.FirstName, @event.LastName,
                     *                                  @event.ContactEmail, @event.ContactTelephone1, @event.ContactTelephone2, @event.OnlineAccessEnbaled,
                     *                                  @event.UserAvartaImgUrl, @event.IsActive, @event.RoleId, @event.Notes, @event.StreetNumber,
                     *                                  @event.City, @event.StateProv, @event.ZipPostCode, @event.Country);
                     *
                     *
                     * try
                     * {
                     *  await _messagePublisher.PublishMessageAsync(e.MessageType, e, "asset_created"); // publishing the message
                     *  Log.Information("Message  {MessageType} with Id {MessageId} has been published successfully", e.MessageType, e.MessageId);
                     * }
                     * catch (Exception ex)
                     * {
                     *  Log.Error(ex, "Error while publishing {MessageType} message with id {MessageId}.", e.MessageType, e.MessageId);
                     * }
                     *
                     */
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "Error while addinng owner {Owner} to {Property}.", @event.FirstName + " " + @event.LastName, rentalProperty.PropertyName);
                    throw ex;
                }
            }



            //throw new NotImplementedException();
        }
Exemplo n.º 2
0
        public async Task <AddImageToPropertyViewModel> Handle(AddImageToPropertyCommand request, CancellationToken cancellationToken)
        {
            var file = request.PropertyImage;

            string path        = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\images\\properties\\");
            string ext         = Path.GetExtension(file.Name);
            string newFileName = "rental_property_" + request.RentalPropertyId.ToString() + "." + ext;

            string url = "images/properties/" + file.FileName;

            if (file.Length > 0)
            {
                using (var fileStream = new FileStream(Path.Combine(path, file.FileName), FileMode.Create))
                {
                    try
                    {
                        await file.CopyToAsync(fileStream);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }

            var addedImage = new AddImageToPropertyViewModel();

            var property = _context.RentalProperty.FirstOrDefault(p => p.Id == request.RentalPropertyId);

            var image = property.AddImages(request.PropertyImgTitle, url, request.RentalPropertyId);


            addedImage.PropertyImgUrl   = image.PropertyImgUrl;
            addedImage.PropertyImgTitle = image.PropertyImgTitle;
            addedImage.RentalPropertyId = image.RentalPropertyId;

            _context.Add(image);

            try
            {
                await _context.SaveChangesAsync();

                addedImage.Id = image.Id;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(addedImage);
        }