public override bool Execute() { int total = accountServices.GetAllUserProfiles().Count(); int totale = eventServices.GetAllEvents().Count(); int s,r,e; do { s = random.Next(total); r = random.Next(total); e = random.Next(totale); } while (s==r); MessageModel newMessage = new MessageModel { Message = "New Random Message", ReceipientProfileID = r, SenderProfileID = s, EventID = e }; return eventServices.SendMessage(newMessage); }
//same as above, pero walang db.save changes private bool _sendMessage(MessageModel messageModel) { if (messageModel.ReceipientProfileID < 1 || messageModel.SenderProfileID < 1) { return false; } UserProfile _sender = db.UserProfiles.Where(x => x.UserProfileId == messageModel.SenderProfileID).FirstOrDefault(); UserProfile _recipient = db.UserProfiles.Where(x => x.UserProfileId == messageModel.ReceipientProfileID).FirstOrDefault(); if (_sender == null || _recipient == null) { return false; } Event _event = db.Events.Where(ev => ev.EventId == messageModel.EventID).FirstOrDefault(); Mail newMail = new Mail { DateSent = DateTime.Now, FromUserProfile = _sender, MailMessage = messageModel.Message, Read = false, ReferredEvent = _event, Starred = false, ToUserProfile = _recipient }; db.Mails.AddObject(newMail); return true; }
public bool Share(ShareEventModel shareEventModel) { Event _event = db.Events.Where(ev => ev.EventId == shareEventModel.EventID).FirstOrDefault(); UserProfile _profile = db.UserProfiles.Where(up => up.UserProfileId == shareEventModel.SharerProfileId).FirstOrDefault(); IEnumerable<UserProfile> _recipients = db.UserProfiles.Where(rec => shareEventModel.SharedProfileIDList.Contains(rec.UserProfileId)); if (_profile == null || _recipients.Count() <= 0) return false; foreach (UserProfile _recipient in _recipients) { MessageModel mm = new MessageModel { Message = shareEventModel.Message, SenderProfileID = shareEventModel.SharerProfileId, ReceipientProfileID = _recipient.UserProfileId }; if (_event != null) { mm.EventID = shareEventModel.EventID; } _sendMessage(mm); /*db.Mails.AddObject( new Mail { MailMessage = shareEventModel.Message, FromUserProfile = _profile, ReferredEvent = _event, ToUserProfile = _recipient, Read = false, Starred = false });*/ } db.SaveChanges(); return true; }