Example #1
0
        public async Task <bool> SubscribeAsync(AddSubscribeRequestModel model, int userId, string deviceId, Language language)
        {
            Guest guest = null;
            SubscribeAnnouncement subscribeAnnouncement = null;
            var user = await repository.FilterAsNoTracking <User>(u => u.Id == userId).FirstOrDefaultAsync();

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

                if (guest == null)
                {
                    throw new Exception(optionsBinder.Error().NoGuest);
                }
            }
            var announcement = await repository.FilterAsNoTracking <Announcement>(a => a.Id == model.AnnouncementId).FirstOrDefaultAsync();

            if (announcement == null)
            {
                throw new Exception(optionsBinder.Error().AnnouncementNotFound);
            }
            if (user != null)
            {
                subscribeAnnouncement = await repository.Filter <SubscribeAnnouncement>(s => s.UserId == user.Id &&
                                                                                        s.AnnouncementId == announcement.Id).FirstOrDefaultAsync();
            }
            else
            {
                subscribeAnnouncement = await repository.Filter <SubscribeAnnouncement>(s => s.GuestId == guest.Id &&
                                                                                        s.AnnouncementId == announcement.Id).FirstOrDefaultAsync();
            }
            if (subscribeAnnouncement != null)
            {
                throw new Exception("Subscribe Announcement already exist");
            }

            repository.Create(new SubscribeAnnouncement
            {
                User                   = user ?? null,
                Guest                  = guest ?? null,
                AnnouncementId         = announcement.Id,
                Email                  = model.Email,
                Address                = announcement.AddressEn,
                AnnouncementType       = announcement.AnnouncementType,
                AnnouncementEstateType = announcement.AnnouncementEstateType
            });
            await repository.SaveChangesAsync();

            return(true);
        }
 public async Task <IActionResult> Subscribe([FromBody] AddSubscribeRequestModel model)
 {
     return(await MakeActionCallAsync(async() =>
                                      await subscribeService.SubscribeAsync(model, GetPersonId(), GetDeviceId(), GetLanguage())));
 }