コード例 #1
0
        public HttpResponseMessage SendMessage(MessageResponceModel message,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))]
            string accessToken)
        {
                var data = new MessagesRepository(
                        ConfigurationManager.AppSettings["MongoConnectionString"],
                        ConfigurationManager.AppSettings["Database"]);
                var user = this.GetUserByAccessToken(accessToken, data.Db);

                if (user == null)
                {
                    return this.Request.CreateResponse(HttpStatusCode.Unauthorized);
                }

                message.Sender = user.Username + "@" + MailDomain;
                ValidateMessage(message);
                SendMessageToMailgun(message);
                DbMessageModel dbMessage = new DbMessageModel()
                {
                    BodyHtml = message.BodyHtml,
                    BodyPlain = message.BodyPlain,
                    Date = message.Date,
                    Recipient = message.Recipient,
                    Sender = message.Sender,
                    Subject = message.Subject,
                    Username = user.Username
                };

                data.SaveMessage(dbMessage);
                var response = this.Request.CreateResponse(HttpStatusCode.Created, message);
                return response;
        }
コード例 #2
0
        public HttpResponseMessage ReceiveMessage()
        {
            return this.ExecuteOperationAndHandleExceptions(() =>
            {
                HttpResponseMessage result = null;
                var httpRequest = HttpContext.Current.Request;

                var data = new MessagesRepository(
                    ConfigurationManager.AppSettings["MongoConnectionString"],
                    ConfigurationManager.AppSettings["Database"]);

                var recipient = httpRequest.Unvalidated.Form["recipient"].Split('@');
                var recipientName = recipient[0].Trim();

                DbMessageModel model = new DbMessageModel()
                {

                    BodyHtml = httpRequest.Unvalidated.Form["body-html"],
                    BodyPlain = httpRequest.Unvalidated.Form["body-plain"],
                    Date = DateTime.Now,
                    Sender = httpRequest.Unvalidated.Form["from"],
                    Recipient = httpRequest.Unvalidated.Form["recipient"],
                    Subject = httpRequest.Unvalidated.Form["subject"],
                    Username = recipientName
                };

                data.Add(model);

                var responseModel = new MessageResponceModel
                {
                    Id = model.Id,
                    BodyHtml = model.BodyHtml,
                    BodyPlain = model.BodyPlain,
                    Date = model.Date,
                    Recipient = model.Recipient,
                    Sender = model.Sender,
                    Subject = model.Subject
                };

                var response = this.Request.CreateResponse(HttpStatusCode.Created, responseModel);
                return response;
            });
        }
コード例 #3
0
        public HttpResponseMessage GetMessage(string messageId,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))]
            string accessToken)
        {
            var data = new MessagesRepository(
                    ConfigurationManager.AppSettings["MongoConnectionString"],
                    ConfigurationManager.AppSettings["Database"]);
            var user = this.GetUserByAccessToken(accessToken, data.Db);

            if (user == null)
            {
                return this.Request.CreateResponse(HttpStatusCode.Unauthorized);
            }

            var message = data.GetMessage(messageId);
            if (message == null)
            {
                return this.Request.CreateResponse(HttpStatusCode.BadRequest, messageId);
            }

            var responceMessage = new MessageResponceModel()
                                 {
                                     BodyHtml = message.BodyHtml,
                                     BodyPlain = message.BodyPlain,
                                     Date = message.Date,
                                     Id = message.Id,
                                     Recipient = message.Recipient,
                                     Sender = message.Sender,
                                     Subject = message.Subject
                                 };

            var response = this.Request.CreateResponse(HttpStatusCode.OK, responceMessage);
            return response;
        }
コード例 #4
0
        public HttpResponseMessage DeleteMessage(string messageId,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))]
            string accessToken)
        {
            var data = new MessagesRepository(
                    ConfigurationManager.AppSettings["MongoConnectionString"],
                    ConfigurationManager.AppSettings["Database"]);

            var user = this.GetUserByAccessToken(accessToken, data.Db);

            if (user == null)
            {
                return this.Request.CreateResponse(HttpStatusCode.Unauthorized);
            }

            var message = data.GetMessage(messageId);
            if (message == null)
            {
                return this.Request.CreateResponse(HttpStatusCode.BadRequest, messageId);
            }

            data.DeleteMessage(messageId);

            var response = this.Request.CreateResponse(HttpStatusCode.OK);
            return response;

        }
コード例 #5
0
        public HttpResponseMessage GetFolder(string folder,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))]
            string accessToken) 
        {
            var data = new MessagesRepository(
                    ConfigurationManager.AppSettings["MongoConnectionString"],
                    ConfigurationManager.AppSettings["Database"]);
            var user = this.GetUserByAccessToken(accessToken, data.Db);

            if (user == null)
            {
                return this.Request.CreateResponse(HttpStatusCode.Unauthorized);
            }

            IQueryable<DbMessageModel> dbFolder = null;

            switch (folder)
            {
                case "Inbox":
                    dbFolder = data.GetInbox(user);
                    break;
                case "Sent":
                    dbFolder = data.GetSentItems(user);
                    break;
                case "Trash":
                    dbFolder = data.GetTrash(user);
                    break;
                default:

                    break;
            }
            var responceFolder = from message in dbFolder
                                select new FolderResponceModel()
                                {
                                    Date = message.Date,
                                    Id = message.Id,
                                    Recipient = message.Recipient,
                                    Sender = message.Sender,
                                    Subject = message.Subject
                                };

            var response = this.Request.CreateResponse(HttpStatusCode.OK, responceFolder);
            return response;
        }