Ejemplo n.º 1
0
        public HttpResponseMessage AddTopic(string message, string from, IEnumerable <string> to, IEnumerable <HttpPostedFileBase> files)
        {
            try
            {
                var errorMessage = string.Empty;
                var attachments  = docUtils.UploadFiles(from, files, ref errorMessage);

                if (!string.IsNullOrWhiteSpace(errorMessage))
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Error uploading files: " + errorMessage));
                }

                TasksService.CreateTopic(new CreateTopicRequest
                {
                    Message     = message,
                    From        = from,
                    To          = String.Join(",", to),
                    Attachments = attachments
                });

                return(new HttpResponseMessage(HttpStatusCode.Created));
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
Ejemplo n.º 2
0
        public HttpResponseMessage AddTopic(string from, string to)
        {
            try
            {
                var toList       = ((string.IsNullOrEmpty(to) ? "" : to) + ",").Split(',').Where(t => !string.IsNullOrEmpty(t)).ToList();
                var message      = HttpContext.Current.Request.Form["message"];
                var errorMessage = string.Empty;
                IEnumerable <TopicAttachmentInfo> attachments = null;
                if (HttpContext.Current.Request.Files.Count > 0)
                {
                    var files = Enumerable.Range(0, HttpContext.Current.Request.Files.Count).Select(
                        t =>
                    {
                        HttpPostedFileBase filebase = new HttpPostedFileWrapper(HttpContext.Current.Request.Files[t]);
                        return(filebase);
                    });

                    attachments = docUtils.UploadFiles(from, files, ref errorMessage);
                }

                var resp = TasksService.CreateTopic(new CreateTopicRequest
                {
                    Message     = message,
                    From        = from,
                    To          = to,
                    Attachments = attachments
                });

                var fromUser = UsersService.GetUser(new GetUserRequest {
                    User = from
                });
                if (fromUser.User == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                var content = new AddTopicModel
                {
                    Result  = true,
                    TopicId = resp.TopicId,
                    Message = new TopicModelItem(new TopicMessageInfo
                    {
                        From        = fromUser.User.UserName,
                        To          = "",
                        ImageUrl    = fromUser.User.PhotoPath,
                        When        = DateTime.Now,
                        Message     = message,
                        Attachments = attachments == null ? new List <TopicAttachmentInfo>() : attachments,
                        Status      = TopicStatusType.New
                    }),
                    Replies = new List <TopicMessageInfo>().ToArray()
                };

                var result = Request.CreateResponse(HttpStatusCode.OK);
                var json   = JsonConvert.SerializeObject(
                    content,
                    Formatting.Indented,
                    new JsonSerializerSettings {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                }
                    );
                result.Content = new StringContent(json, Encoding.UTF8, "text/plain");
                return(result);
            }
            catch (Exception e)
            {
                var content = new AddTopicModel {
                    Result = false, ErrorMessage = e.Message
                };
                var json = JsonConvert.SerializeObject(
                    content,
                    Formatting.Indented,
                    new JsonSerializerSettings {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                }
                    );
                var result = Request.CreateResponse(HttpStatusCode.OK);
                result.Content = new StringContent(json, Encoding.UTF8, "text/plain");
                return(result);
            }
        }