예제 #1
0
 private void Seed()
 {
     //SITE Initilization
     if (!_context.Book.Any())
     {
         _context.AddRange(bookCollections);
         _context.SaveChanges();
     }
 }
예제 #2
0
        public async Task <IActionResult> SubmitMessage([FromBody] MessageModel model)
        {
            if (model.Message.Length > 160 || model.Customers.Count == 0)
            {
                if (model.Message.Length > 160)
                {
                    Log.Information($"Predugacka poruka poslana");
                }
                return(BadRequest());
            }

            Log.Information($"Nova poruka poslana");

            //Dohvati cutomere koji su u popisu id-eva u requestu
            List <Customer> customers = await demoDbContext.Customers
                                        .Where(x => model.Customers.Contains(x.Id))
                                        .ToListAsync();

            //Dohvati postojece poruke za customere iz requesta
            List <Message> messagesExisting = await demoDbContext.Messages
                                              .Where(x => customers.Select(x => x.Id).Contains(x.CustomerId))
                                              .ToListAsync();

            List <Message> messagesNew = new List <Message>();



            string path = Directory.GetCurrentDirectory();

            path += "\\sms";

            Directory.CreateDirectory(path);

            byte[] fileContent = Encoding.UTF8.GetBytes(model.Message);

            FileStream fileStream = null;

            foreach (Customer customer in customers)
            {
                string fileName     = $"demo_{customer.PhoneNumber}.txt";
                string fullFilePath = $"{path}\\{fileName}";

                System.IO.File.WriteAllText(fullFilePath, model.Message);

                bool msgExists = false;

                //Cudno, ali OK...
                foreach (Message message in messagesExisting)
                {
                    if (message.FileName == fileName)
                    {
                        message.DateTimeSent = DateTime.Now;
                        msgExists            = true;
                    }
                }

                if (msgExists == false)
                {
                    Message messageTemp = new Message
                    {
                        CustomerId   = customer.Id,
                        DateTimeSent = DateTime.Now,
                        FileName     = fileName
                    };

                    messagesNew.Add(messageTemp);
                }
            }

            demoDbContext.AddRange(messagesNew);
            demoDbContext.UpdateRange(messagesExisting);

            await demoDbContext.SaveChangesAsync();

            return(Ok());
        }