예제 #1
0
 public async Task <IActionResult> GetList([FromBody] MessagePagingRequestModel model)
 {
     return(await MakeActionCallAsync(async() => await _service.GetList(model, GetPersonId())));
 }
예제 #2
0
        public async Task <PagingResponseMessageModel> GetList(MessagePagingRequestModel model, int userId)
        {
            AnnouncementViewModel announcement = null;
            var caller = await _repository.Filter <User>(u => u.Id == userId).FirstOrDefaultAsync();

            if (caller == null)
            {
                throw new Exception("caller not found");
            }
            var conversation = await _repository.Filter <Conversation>(x => x.Id == model.ConversationId)
                               .Where(x => (x.AnnouncementCreatorId == caller.Id) || x.QuestionerId == caller.Id).FirstOrDefaultAsync();

            int announcementCount = 0;

            if (conversation.QuestionerId == caller.Id)
            {
                announcementCount = await _repository.Filter <Announcement>(x => !x.IsDraft && x.UserId == conversation.AnnouncementCreatorId).CountAsync();
            }
            else
            {
                announcementCount = await _repository.Filter <Announcement>(x => !x.IsDraft && x.UserId == conversation.QuestionerId).CountAsync();
            }

            Currency c = _repository.Filter <Currency>(c => c.Id == caller.CurrencyId).FirstOrDefault();

            var deletedMessages = _repository.Filter <Message>(s => s.ConversationId == conversation.Id)
                                  .Where(x => x.SenderId == caller.Id && x.SenderMessageIsDeleted ||
                                         x.ReciverId != caller.Id && x.ReciverMessageIsDeleted).Select(s => s.Id);
            var query = _repository.Filter <Message>(x => x.ConversationId == conversation.Id)
                        .Where(s => !deletedMessages.Contains(s.Id));

            if (model.Page == 1)
            {
                announcement = await _repository.Filter <Announcement>(x => !x.IsDraft && x.Id == conversation.AnnouncementId)
                               .Skip((model.Page - 1) * model.Count).Take(model.Count).Select(
                    x => new AnnouncementViewModel
                {
                    Id = x.Id,
                    AnnouncementType            = x.AnnouncementType,
                    AnnouncementRentType        = x.AnnouncementRentType,
                    AnnouncementResidentialType = x.AnnouncementResidentialType,
                    AnnouncementEstateType      = x.AnnouncementEstateType,
                    AnnouncementStatus          = x.AnnouncementStatus,
                    Area                  = Convert.ToInt64(x.Area),
                    BathroomCount         = x.BathroomCount,
                    BedroomCount          = x.BedroomCount,
                    Address               = x.AddressEn.Trim(),
                    Price                 = Convert.ToInt64(x.Price),
                    Title                 = x.Title,
                    UserAnnouncementCount = announcementCount,
                    UserId                = x.UserId,
                    UserName              = x.User.FullName,
                    CurrencyId            = x.CurrencyId,

                    Description        = x.Description,
                    TitleArabian       = x.TitleArabian,
                    CountryId          = x.CountryId,
                    CityId             = x.CityId,
                    SittingCount       = x.SittingCount,
                    ConstructionStatus = x.ConstructionStatus,
                    SaleType           = x.SaleType,
                    FurnishingStatus   = x.FurnishingStatus,
                    OwnerShip          = x.OwnerShip,
                    BuildingAge        = x.BuildingAge,
                    CommercialType     = x.CommercialType,
                    LandType           = x.LandType,
                    FacadeType         = x.FacadeType,
                    DisctrictName      = x.DisctrictName,
                    ShareUrl           = $"https://baitkm.com/products/details/{x.Id}",
                    Lat                = x.Lat,
                    Lng                = x.Lng,
                    CreateDate         = x.CreatedDt,
                    BalconyArea        = x.BalconyArea,
                    KitchenArea        = x.KitchenArea,
                    MeterPrice         = x.MeterPrice,
                    LaundryArea        = x.LaundryArea,
                    LivingArea         = x.LivingArea,
                    CurrencySymbol     = c.Symbol,
                    LandNumber         = x.LandNumber,
                    PlanNumber         = x.PlanNumber,
                    CurrencyCode       = c.Code,
                    StreetWidth        = x.StreetWidth,
                    NumberOfAppartment = x.NumberOfAppartment,
                    NumberOfFloors     = x.NumberOfFloors,
                    CreatedDt          = x.CreatedDt,
                    NumberOfVilla      = x.NumberOfVilla,
                    OfficeSpace        = x.OfficeSpace,
                    LaborResidence     = x.LaborResidence,
                    District           = x.District,
                    NumberOfWareHouse  = x.NumberOfWareHouse,
                    NumberOfShop       = x.NumberOfShop,
                    NumberOfUnits      = x.NumberOfUnits,
                    FireSystem         = x.FireSystem,
                    LandCategory       = x.LandCategory,
                    UserProfilePhoto   = new ImageOptimizer
                    {
                        Photo = Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                         UploadType.ProfilePhoto, x.User.ProfilePhoto, ConstValues.Width, ConstValues.Height, false, 0)
                    },
                    Photo = new ImageOptimizer
                    {
                        Photo = Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                         UploadType.AnnouncementBasePhoto, x.BasePhoto, ConstValues.Width, ConstValues.Height, false, 0),
                        PhotoBlur = Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                             UploadType.AnnouncementBasePhoto, x.BasePhoto, 100, 100, true, 0)
                    },
                    IsDeleted = x.IsDeleted,
                }).FirstOrDefaultAsync();

                if (announcement != null)
                {
                    announcement.CurrencyCode   = c.Code;
                    announcement.CurrencySymbol = c.Symbol;
                    if (c.Id != 1)
                    {
                        decimal currentRate = _repository.Filter <Rate>(r => r.CurrencyId == c.Id).FirstOrDefault().CurrentRate;
                        announcement.Price /= currentRate;
                    }

                    City city = _repository.Filter <City>(c => c.Id == announcement.CityId).Include(c => c.Country).FirstOrDefault();
                    announcement.City    = city.Name;
                    announcement.Country = city.Country.Name;

                    if (announcement.Photo.Photo == null && announcement.AnnouncementResidentialType.HasValue)
                    {
                        announcement.Photo = new ImageOptimizer {
                            Photo = DefaultCoverImgePath(announcement.AnnouncementResidentialType.Value)
                        }
                    }
                    ;
                }
            }

            var count = await query.CountAsync();

            var page = Convert.ToInt32(Math.Ceiling(decimal.Divide(count, model.Count)));

            if (model.Page > 1)
            {
                query = query.Where(x => x.CreatedDt < model.DateFrom);
            }
            var result = await query.OrderByDescending(x => x.CreatedDt).Skip((model.Page - 1) * model.Count)
                         .Take(model.Count).Select(x => new MessageListModel
            {
                MessageBodyType = x.MessageBodyType,
                Photo           = x.User != null
                        ? new ImageOptimizer
                {
                    Photo = Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                     UploadType.ProfilePhoto, x.User.ProfilePhoto, 300, 300, false, 0),
                    PhotoBlur = Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                         UploadType.ProfilePhoto, x.User.ProfilePhoto, 100, 100, true, 0)
                } : new ImageOptimizer(),
                SenderId    = x.SenderId,
                FullName    = x.User != null ? x.User.FullName : "User",
                MessageId   = x.Id,
                MessageText = x.MessageBodyType == MessageBodyType.Image
                        ? Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                   UploadType.MessageFiles, x.MessageText, 1000, 1000, false, conversation.Id)
                        : x.MessageText,
                CreatedDate  = x.CreatedDt,
                IsSentFromMe = x.SenderId == caller.Id,
                FileUrl      = x.MessageBodyType == MessageBodyType.File
                        ? Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaDownload,
                                                   UploadType.MessageFiles, x.MessageText, false, conversation.Id)
                        : null,
                FileSize      = x.FileLength,
                ReplayMessage = x.ReplayMessageId != null ?
                                query.Where(s => s.Id == x.ReplayMessageId.GetValueOrDefault())
                                .Select(s => new MessageListModel
                {
                    MessageBodyType = s.MessageBodyType,
                    Photo           = s.User != null ? new ImageOptimizer
                    {
                        Photo = Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                         UploadType.ProfilePhoto, s.User.ProfilePhoto, 300, 300, false, 0),
                        PhotoBlur = Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                             UploadType.ProfilePhoto, s.User.ProfilePhoto, 100, 100, true, 0)
                    } : new ImageOptimizer(),
                    SenderId    = s.SenderId,
                    FullName    = s.User != null ? s.User.FullName : "User",
                    MessageId   = s.Id,
                    MessageText = s.MessageBodyType == MessageBodyType.Image ?
                                  Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                           UploadType.MessageFiles, s.MessageText, 1000, 1000, false, conversation.Id) : s.MessageText,
                    CreatedDate  = s.CreatedDt,
                    IsSentFromMe = s.SenderId == caller.Id,
                    FileUrl      = s.MessageBodyType == MessageBodyType.File ?
                                   Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaDownload,
                                                            UploadType.MessageFiles, s.MessageText, false, conversation.Id) : null,
                    FileSize = s.FileLength
                }).FirstOrDefault() : null
            }).ToListAsync();

            var unSeen = await _repository
                         .Filter <Message>(x => !x.IsSeen && x.ConversationId == conversation.Id && x.SenderId != caller.Id)
                         .ToListAsync();

            foreach (var message in unSeen)
            {
                message.IsSeen = true;
                _repository.Update(message);
            }
            await _repository.SaveChangesAsync();

            return(new PagingResponseMessageModel
            {
                Announcement = announcement,
                ItemCount = count,
                PageCount = page,
                DateFrom = model.Page == 1 ? result.FirstOrDefault()?.CreatedDate : model.DateFrom,
                Data = result
            });
        }
예제 #3
0
        public async Task <PagingResponseModel <SupportMessageListModel> > GetMobileList(MessagePagingRequestModel model,
                                                                                         int userId, Language language, string deviceId)
        {
            Currency currency;
            Guest    guest  = null;
            var      caller = await _repository.FilterAsNoTracking <User>(u => u.Id == userId).FirstOrDefaultAsync();

            if (caller == null)
            {
                guest = await _repository.FilterAsNoTracking <Guest>(g => g.DeviceId == deviceId).FirstOrDefaultAsync();

                if (guest == null)
                {
                    throw new Exception(_optionsBinder.Error().UserNotFound);
                }
                currency = await _repository.Filter <Currency>(c => c.Id == guest.CurrencyId).FirstOrDefaultAsync();
            }
            else
            {
                currency = await _repository.Filter <Currency>(c => c.Id == caller.CurrencyId).FirstOrDefaultAsync();
            }

            var conversation = await _repository.Filter <SupportConversation>(x => x.Id == model.ConversationId)
                               .FirstOrDefaultAsync();

            if (conversation == null)
            {
                throw new Exception("conversation not found");
            }
            var callerId = caller?.Id ?? guest.Id;
            var query    = _repository.Filter <SupportMessage>(x => x.SupportConversationId == conversation.Id);
            var count    = await query.CountAsync();

            var page = Convert.ToInt32(Math.Ceiling(decimal.Divide(count, model.Count)));

            if (model.Page > 1)
            {
                query = query.Where(x => x.CreatedDt < model.DateFrom);
            }
            var data = await query.OrderByDescending(x => x.CreatedDt)
                       .Skip((model.Page - 1) * model.Count).Take(model.Count)
                       .Select(x => new SupportMessageListModel
            {
                MessageId   = x.Id,
                MessageText = x.SupportMessageBodyType == SupportMessageBodyType.Image
                                    ? Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                               UploadType.SupportConversationFiles, x.MessageText, 400, 400, false, conversation.Id) : x.MessageText,
                ConversationId  = x.SupportConversationId,
                IsSentFromMe    = (x.UserSenderId ?? x.GuestSenderId ?? 0) == callerId,
                MessageBodyType = x.SupportMessageBodyType,
                CreatedDate     = x.CreatedDt,
                SenderId        = x.UserSenderId ?? x.GuestSenderId ?? 0,
                FullName        = x.UserSender.FullName ?? x.GuestSenderId.ToString() ?? null,
                Photo           = x.UserSender != null
                                    ? new ImageOptimizer
                {
                    Photo = Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                     UploadType.ProfilePhoto, x.UserSender.ProfilePhoto, 500, 500, false, 0),
                    PhotoBlur = Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                         UploadType.ProfilePhoto, x.UserSender.ProfilePhoto, 100, 100, true, 0)
                }
                                    : new ImageOptimizer(),
                FileUrl = x.SupportMessageBodyType == SupportMessageBodyType.Photo
                                    ? Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaDownload,
                                                               UploadType.SupportConversationFiles, x.MessageText, false, conversation.Id)
                                    : null,
                FileSize      = x.FileLength,
                ReplayMessage = x.ReplayMessageId != null ?
                                query.Where(w => w.Id == x.ReplayMessageId)
                                .Select(s => new SupportMessageListModel
                {
                    MessageId   = s.Id,
                    MessageText = s.SupportMessageBodyType == SupportMessageBodyType.Image
                                                 ? Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                                            UploadType.SupportConversationFiles, s.MessageText, 400, 400, false, conversation.Id) : s.MessageText,
                    ConversationId  = s.SupportConversationId,
                    IsSentFromMe    = (s.UserSenderId ?? s.GuestSenderId ?? 0) == callerId,
                    MessageBodyType = s.SupportMessageBodyType,
                    CreatedDate     = s.CreatedDt,
                    SenderId        = s.UserSenderId ?? s.GuestSenderId ?? 0,
                    FullName        = s.UserSender.FullName ?? s.GuestSenderId.ToString() ?? null,
                    Photo           = s.UserSender != null ? new ImageOptimizer
                    {
                        Photo = Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                         UploadType.ProfilePhoto, s.UserSender.ProfilePhoto, 500, 500, false, 0),
                        PhotoBlur = Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                             UploadType.ProfilePhoto, s.UserSender.ProfilePhoto, 100, 100, true, 0)
                    } : new ImageOptimizer(),


                    FileUrl = x.SupportMessageBodyType == SupportMessageBodyType.Photo ?
                              Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaDownload,
                                                       UploadType.SupportConversationFiles, x.MessageText, false, conversation.Id) : null,
                    FileSize = x.FileLength,
                }).FirstOrDefault() : null
            }).ToListAsync();

            foreach (var variable in data)
            {
                if (variable.MessageBodyType != SupportMessageBodyType.Announcement)
                {
                    if (variable.ReplayMessage != null)
                    {
                        if (variable.ReplayMessage.MessageBodyType == SupportMessageBodyType.Announcement)
                        {
                            int.TryParse(variable.ReplayMessage.MessageText, out var announcementId);
                            var replayAnnouncement = await _repository.Filter <Announcement>(x => !x.IsDraft && x.Id == announcementId)
                                                     .Select(x => new AnnouncementListViewModel
                            {
                                Id            = x.Id,
                                BathroomCount = x.BathroomCount,
                                AnnouncementResidentialType = x.AnnouncementResidentialType,
                                AnnouncementRentType        = x.AnnouncementRentType,
                                IsFavourite            = false,
                                Address                = x.AddressEn.Trim(),
                                AnnouncementEstateType = x.AnnouncementEstateType,
                                AnnouncementType       = x.AnnouncementType,
                                Area         = Convert.ToInt64(x.Area),
                                BedroomCount = x.BedroomCount,
                                Price        = Convert.ToInt64(x.Price),
                                CurrencyId   = x.CurrencyId,
                                Title        = x.Title,
                                CreateDate   = x.CreatedDt,
                                Photo        = new ImageOptimizer
                                {
                                    Photo = Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                                     UploadType.AnnouncementBasePhoto, x.BasePhoto, 500, 500, false, 0),
                                    PhotoBlur = Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                                         UploadType.AnnouncementBasePhoto, x.BasePhoto, 100, 100, true, 0)
                                }
                            }).FirstOrDefaultAsync();

                            variable.ReplayMessage.Announcement = replayAnnouncement != null ? replayAnnouncement : null;
                            if (variable.ReplayMessage.Announcement != null)
                            {
                                replayAnnouncement.CurrencyCode = currency.Code;
                                if (currency.Id != 1)
                                {
                                    decimal currentRate = _repository.Filter <Rate>(rate => rate.CurrencyId == currency.Id).FirstOrDefault().CurrentRate;
                                    replayAnnouncement.Price /= currentRate;
                                }
                            }
                        }
                    }
                    continue;
                }
                int.TryParse(variable.MessageText, out var id);
                var announcement = await _repository.Filter <Announcement>(x => !x.IsDraft && x.Id == id &&
                                                                           !x.IsDeleted).Select(x =>
                                                                                                new AnnouncementListViewModel
                {
                    Id            = x.Id,
                    BathroomCount = x.BathroomCount,
                    AnnouncementResidentialType = x.AnnouncementResidentialType,
                    AnnouncementRentType        = x.AnnouncementRentType,
                    IsFavourite            = false,
                    Address                = x.AddressEn.Trim(),
                    AnnouncementEstateType = x.AnnouncementEstateType,
                    AnnouncementType       = x.AnnouncementType,
                    Area         = Convert.ToInt64(x.Area),
                    CurrencyId   = x.CurrencyId,
                    BedroomCount = x.BedroomCount,
                    Price        = Convert.ToInt64(x.Price),
                    Title        = x.Title,
                    CreateDate   = x.CreatedDt,
                    Photo        = new ImageOptimizer
                    {
                        Photo = Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                         UploadType.AnnouncementBasePhoto, x.BasePhoto, 500, 500, false, 0),
                        PhotoBlur = Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                             UploadType.AnnouncementBasePhoto, x.BasePhoto, 100, 100, true, 0)
                    }
                }).FirstOrDefaultAsync();

                if (announcement != null)
                {
                    announcement.CurrencyCode = currency.Code;
                    if (currency.Id != 1)
                    {
                        decimal currentRate = _repository.Filter <Rate>(rate => rate.CurrencyId == currency.Id).FirstOrDefault().CurrentRate;
                        announcement.Price /= currentRate;
                    }
                    //if (announcement.Photo == null && announcement.AnnouncementResidentialType.HasValue)
                    //    announcement.Photo = new ImageOptimizer { Photo = DefaultCoverImgePath(announcement.AnnouncementResidentialType.Value) };
                    //if (announcement.Photo.Photo == null && announcement.CommercialType.HasValue)
                    //    announcement.Photo = new ImageOptimizer { Photo = DefaultCommercialIMagePath(announcement.CommercialType.Value) };
                    //if (announcement.Photo.Photo == null && announcement.AnnouncementEstateType == AnnouncementEstateType.Land)
                    //    announcement.Photo = new ImageOptimizer { Photo = DefaultLandMagePath() };
                }
                if (announcement == null && variable.MessageBodyType == SupportMessageBodyType.Announcement)
                {
                    continue;
                }
                else
                {
                    variable.Announcement = announcement;
                }
            }
            var unSeenQuery = _repository
                              .Filter <SupportMessage>(x => !x.IsSeen && x.SupportConversationId == conversation.Id);

            unSeenQuery = caller != null
                ? unSeenQuery.Where(x => x.UserSenderId != caller.Id)
                : unSeenQuery.Where(x => x.GuestSenderId != guest.Id);

            var unSeen = await unSeenQuery.ToListAsync();

            foreach (var message in unSeen)
            {
                message.IsSeen = true;
                _repository.Update(message);
            }
            await _repository.SaveChangesAsync();

            return(new PagingResponseModel <SupportMessageListModel>
            {
                DateFrom = model.Page == 1 ? data.FirstOrDefault()?.CreatedDate : model.DateFrom,
                ItemCount = count,
                Data = data,
                PageCount = page
            });
        }