Exemplo n.º 1
0
        public IHttpActionResult PostTicket(UserTicket uTic)
        {
            //if (!ModelState.IsValid)
            //{
            //    return BadRequest(ModelState);
            //}

            if (uTic == null)
            {
                return(BadRequest(ModelState));
            }

            //JavaScriptSerializer j = new JavaScriptSerializer();
            //UserTicket uTic = (UserTicket)j.Deserialize(uTicStr, typeof(UserTicket));



            Ticket ticket = new Ticket();

            //uTic.TicketId = uTic.TicketId;
            ticket.Subject       = uTic.Subject;
            ticket.ConstituentID = uTic.ConstituentID;
            ticket.Service       = uTic.Service;
            ticket.IssueId       = (int)uTic.IssueId;

            ticket.IssueDetailId  = (int)uTic.IssueDetailId;
            ticket.IssueAddInfoId = (int)uTic.IssueAddInfoId;
            ticket.Description    = uTic.Description;

            ticket.TicketStatus = uTic.TicketStatus;
            ticket.DateReported = DateTime.Now.Date;
            ticket.TimeReported = DateTime.Now.TimeOfDay;

            db.Tickets.Add(ticket);
            db.SaveChanges();
            // Only save location if RFS ticket
            if (uTic.TicketType == 1)
            {
                TicketLocation ticLoc = new TicketLocation();
                ticLoc.Latitude  = Convert.ToDouble(ticketLoc.Latitude);
                ticLoc.Longitude = Convert.ToDouble(ticketLoc.Longitude);
                ticLoc.Location  = Convert.ToString(ticketLoc.Address);
                ticLoc.ParcelNo  = Convert.ToString(ticketLoc.ParcelNo);
                ticLoc.CrossSt1  = Convert.ToString(ticketLoc.CrossSt1);
                ticLoc.CrossSt2  = Convert.ToString(ticketLoc.CrossSt2);
                if (ticLoc.Location.Contains("Henderson"))
                {
                    ticLoc.City = "Henderson";
                }
                else
                {
                    ticLoc.City = "Las Vegas";
                }
                ticLoc.State = "NV";
                // Now use the identity of created ticket
                ticLoc.TicketId = ticket.TicketId;
                db.TicketLocations.Add(ticLoc);
                db.SaveChanges();
            }

            // Also store attachments
            if (FileUploadController.postedFiles.Count > 0)
            {
                var fileNameUnique = "";
                foreach (var fileName in FileUploadController.postedFiles)
                {
                    var fileSourcePath = Path.Combine(HttpContext.Current.Server.MapPath("~/TempFiles"), fileName);
                    // Get the complete file path
                    fileNameUnique = ticket.TicketId + fileName;
                    var fileDestPath = Path.Combine(HttpContext.Current.Server.MapPath("~/UploadedFiles"), fileNameUnique);
                    // Save the uploaded file to "UploadedFiles" folder
                    File.Move(fileSourcePath, fileDestPath);
                    // Also store file path in DB
                    TicAttachment ticAttachment = new TicAttachment
                    {
                        //uTic.TicketId = uTic.TicketId;
                        TicketId = ticket.TicketId,
                        FilePath = fileDestPath,
                        FileName = fileNameUnique
                    };

                    db.TicAttachments.Add(ticAttachment);
                    db.SaveChanges();
                }
                //var fileName = FileUploadController.postedFiles.First();



                FileUploadController.postedFiles.Clear();
            }

            //return CreatedAtRoute("DefaultApi", new { id = ticket.TicketId }, ticket);
            return(Ok("OK"));
        }