/// <summary> /// Determine to what degree have we managed to send the SMS messages. The barebones version handles just a few statuses. /// This method can be upgraded to handle the exact numbers ( messages sent compared to messages delivered) /// </summary> /// <param name="sendStatuses"></param> /// <returns></returns> public static BulkDeliveryStatus DetermineMessageDeliveryStatus(Dictionary <Message, bool> sendStatuses) { ///lets presume all messages were not delivered BulkDeliveryStatus status = BulkDeliveryStatus.FAILURE; ///we will use counter variables because that means we only have to loop through the dictionary once. ///and the code is more understandable //////fior simplicity's sake we could use Enumerable.All method but then we looping through the dictionary multiple times unless we create a more complex Predicate... int deliveredCnt = 0; int undeliveredCnt = 0; foreach (var sendStatusMsg in sendStatuses.Values) { if (sendStatusMsg == false) { undeliveredCnt++; } else { deliveredCnt++; } } if (undeliveredCnt > 0 && deliveredCnt > 0) { status = BulkDeliveryStatus.PARTIAL; } else if (deliveredCnt == sendStatuses.Count && undeliveredCnt == 0) { status = BulkDeliveryStatus.SUCCESS; } return(status); }
public JsonResult SendMessages(List <Message> toSend) { if (toSend == null) { return(Json(new SMSResponse() { Status = 404, Message = "Nema poruka za poslati!" })); } SMSResponse response = new SMSResponse(); Dictionary <Message, bool> sendStatus = new Dictionary <Message, bool>(); try { foreach (Message msg in toSend) { sendStatus.Add(msg, msg.SendMessage()); } BulkInsertStatus insStatus = this.msgRep.InsertMessage(toSend.Select(x => x.MapToMessageDB()).ToList()); BulkDeliveryStatus delivStatus = Message.DetermineMessageDeliveryStatus(sendStatus); response = Message.MessageToDeliver(delivStatus, insStatus); } catch (Exception ex) { ///TODO log exception return(Json(new SMSResponse(500, "Error in saving messages"))); } return(Json(response, JsonRequestBehavior.AllowGet)); }
public static SMSResponse MessageToDeliver(BulkDeliveryStatus dlvStat, BulkInsertStatus insStat) { SMSResponse resp = new SMSResponse(); string msg = string.Empty; if (dlvStat == BulkDeliveryStatus.SUCCESS && insStat == BulkInsertStatus.SUCCESS) { resp.Message = "Poruke poslane i zabilježene!"; resp.Status = 200; } else if (dlvStat == BulkDeliveryStatus.SUCCESS && insStat != BulkInsertStatus.SUCCESS) { resp.Message = "Poruke poslane ali njihovo slanje nije zabilježeno!"; resp.Status = 202; } else if (dlvStat == BulkDeliveryStatus.PARTIAL) { resp.Message = "Nisu sve poruke poslane!"; resp.Status = 500; } else { resp.Message = "Poruke nisu poslane!"; resp.Status = 500; } return(resp); }