示例#1
0
        public IActionResult Log([FromBody] Message message)
        {
            var payload = new Payload();

            payload.data  = "ferqw";
            payload.data2 = 5;
            payload.data3 = true;

            var bsonPayload = payload.ToBsonDocument();

            message.Payload = bsonPayload;

            _loggingService.LogMessage(message);

            return(Ok("logged successfully"));
        }
示例#2
0
        public async Task <CreatePropertyCommandResult> Handle(CreatePropertyCommand request, CancellationToken cancellationToken)
        {
            #region Create property aggregate root

            var createdProperty = new CreatePropertyCommandResult();

            var address = new PropertyAddress(request.PropertySuiteNumber,
                                              request.PropertyNumber, request.PropertyStreet,
                                              request.PropertyCity, request.PropertyStateProvince, request.PropertyZipPostCode,
                                              request.PropertyCountry);

            var feature = new PropertyFeature(request.NumberOfBathrooms,
                                              request.NumberOfBathrooms, request.NumberOfLayers,
                                              request.NumberOfParking, request.BasementAvailable,
                                              request.TotalLivingArea, request.IsShared, request.FeatureNotes);


            var facility = new PropertyFacility(request.Stove, request.Refrigerator, request.Dishwasher,
                                                request.AirConditioner, request.Laundry, request.BlindsCurtain, request.Furniture,
                                                request.Tvinternet, request.CommonFacility, request.SecuritySystem, request.UtilityIncluded,
                                                request.FireAlarmSystem, request.FacilityNotes, request.Others);



            var property = new Property(request.PropertyName, request.PropertyDesc, request.Type, request.PropertyManagerUserName,
                                        request.PropertyBuildYear, true, request.IsShared, request.Status,
                                        request.BasementAvailable, DateTime.Now, DateTime.Now, address, facility, feature);


            await _context.AddAsync(property);

            #endregion


            PropertyOwner owner = null;

            if (request.PropertyOwnerId == 0)
            {
                object ownerAddress = null;

                if (!request.IsSameAddress)
                {
                    ownerAddress = new OwnerAddress(request.OwnerStreetNumber, request.OwnerCity, request.OwnerStateProv,
                                                    request.OwnerCountry, request.OwnerZipPostCode);
                }
                else
                {
                    ownerAddress = new OwnerAddress(request.PropertySuiteNumber + " " + request.PropertyNumber + " " + request.PropertyStreet, request.PropertyCity, request.PropertyStateProvince,
                                                    request.PropertyCountry, request.PropertyZipPostCode);
                }

                owner = property.AddOwner("NotSet", request.FirstName, request.LastName, request.ContactEmail,
                                          request.ContactTelephone1, request.ContactTelephone2, request.OnlineAccessEnbaled, request.UserAvartaImgUrl,
                                          request.IsActive, request.RoleId, request.Notes, (OwnerAddress)ownerAddress);


                await _context.AddAsync(owner);
            }
            else
            {
                owner = _context.PropertyOwner.FirstOrDefault(o => o.Id == request.PropertyOwnerId);

                var ownerProperty = property.AddExsitingOwner(owner);

                owner.OwnerProperty.Add(ownerProperty);
            }

            try
            {
                await _context.SaveChangesAsync(); // comment out for testing message sending ONLY

                int PropertyId = property.Id;

                int NewOwnerId = owner.Id;

                request.PropertyId  = property.Id;
                request.CreatedDate = property.Created;
                request.UpdateDate  = property.Modified;

                //Populate return resultEnum.GetName(typeof())
                //
                createdProperty.Id                    = PropertyId;
                createdProperty.PropertyName          = request.PropertyName;
                createdProperty.Type                  = request.Type.ToString();
                createdProperty.Status                = request.Status.ToString();
                createdProperty.PropertyLogoImgUrl    = request.PropertyLogoImgUrl;
                createdProperty.IsShared              = request.IsShared;
                createdProperty.IsActive              = request.IsActive;
                createdProperty.IsBasementSuite       = request.IsBasementSuite;
                createdProperty.CreatedDate           = DateTime.Now.ToString("MMMM dd, yyyy");
                createdProperty.UpdateDate            = DateTime.Now.ToString("MMMM dd, yyyy");
                createdProperty.PropertySuiteNumber   = request.PropertySuiteNumber;
                createdProperty.PropertyStreet        = request.PropertyStreet;
                createdProperty.PropertyCity          = request.PropertyCity;
                createdProperty.PropertyStateProvince = request.PropertyStateProvince;
                createdProperty.PropertyZipPostCode   = request.PropertyZipPostCode;
                createdProperty.PropertyCountry       = request.PropertyCountry;


                Log.Information("Property with id {PropertyName} has been successfully created.", property.PropertyName);



                // Publish Domain Event (MediatR pattern)

                AssetCore.Events.PropertyCreatedEvent domainEvent = new AssetCore.Events.PropertyCreatedEvent(property);

                await _mediator.Publish(domainEvent);



                // Publish Integration Event (RabbitMQ)

                var streetNum = request.PropertySuiteNumber + " " + request.PropertyNumber + " " + request.PropertyStreet;

                //var streetNum = address.PropertySuiteNumber + " " + address.PropertyNumber + " " + address.PropertyStreet;
                // Send message to MQ
                //
                PropertyCreatedEvent e = new PropertyCreatedEvent(Guid.NewGuid(), request.PropertyId, request.PropertyName, request.PropertyManagerUserName,
                                                                  request.PropertyBuildYear, request.Type.ToString(), request.BasementAvailable, request.IsShared, request.NumberOfBedrooms,
                                                                  request.NumberOfBathrooms, request.NumberOfLayers, request.NumberOfParking, request.TotalLivingArea,
                                                                  streetNum, request.PropertyCity, request.PropertyStateProvince, request.PropertyCountry,
                                                                  request.PropertyZipPostCode, NewOwnerId, request.FirstName, request.LastName, request.ContactEmail, request.ContactTelephone1, request.ContactTelephone2,
                                                                  request.OwnerStreetNumber, request.OwnerCity, request.OwnerStateProv, request.OwnerZipPostCode, request.OwnerCountry);

                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);
                }


                // Log message for reconciliation purpose         ******** This part can be replaced by Serilog ***************
                //
                var msgDetails = new MessageDetails();

                msgDetails.PrincicipalId     = e.PropertyId;
                msgDetails.PrincipalType     = "Property";
                msgDetails.PrincipalNameDesc = e.PropertyName;
                msgDetails.OperationType     = "Create";

                var details = msgDetails.ToBsonDocument();

                var msg = new Message(e.MessageId, "Asset Management", details, "asset_created", "asset_created.*", "Publish", DateTime.Now);

                await _loggingService.LogMessage(msg);
            }
            catch (Exception ex)
            {
                //throw ex;
                Log.Error(ex, "Error while creating property, {PropertyName} has not been created.", request.PropertyName);
            }

            return(createdProperty);
            //return new CreatePropertyCommandResult() { };
        }