示例#1
0
        public IHttpActionResult PutProduct(int id, Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != product.Id)
            {
                return(BadRequest());
            }

            //db.Entry(product).State = EntityState.Modified;
            db.MarkAsModified(product);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
示例#2
0
        public HttpResponseMessage SendMessage([FromBody] AspNetUserMessage message)
        {
            try
            {
                if (message.SentDate == DateTime.MinValue)
                {
                    message.SentDate = DateTime.Now;
                }

                // If the receiver user blocked the sender; abort the process
                if (db.AspNetBlockedUsers.Any(o => o.From == message.To && o.To == message.From))
                {
                    throw new Exception($"User {message.From} blocked {message.To}! The message can not be sent.");
                }

                // ReadDate should be enforced as null since the receiver hasnt read the msg yet
                message.ReadDate = null;
                // User is not blocked try to send the message
                db.AspNetUserMessages.Add(message);
                db.SaveChanges();

                return(CreateMessage(200, "OK"));
            }
            catch (Exception e)
            {
                // Log the error to the database
                LogException(e);
                return(CreateMessage(500, " Internal Server Error"));
            }
        }
示例#3
0
        // GET: Image
        public ActionResult Index(int count = 10, int page = 0)
        {
            // If page is lower than 1, reset
            if (page < 0)
            {
                page = -1;
            }

            int total = _db.SourceImages.Count();

            // If page is higher than total pages count
            if (page > total / count)
            {
                page = -1;
            }

            if (page == -1)
            {
                // return empty list which shows warning
                return(View(new ListSourceImageViewModel {
                    Page = 0, ImageItems = new List <ThumbImage>()
                }));
            }

            // Load 30 SourceImages according to current page
            List <SourceImage> sourceImages = _db.SourceImages
                                              .Include(i => i.Thumbnails)
                                              .OrderBy(i => i.ID)
                                              .Skip(page * count)
                                              .Take(count)
                                              .ToList();

            // Save References to thumbnails
            List <ThumbImage> thumbnails = new List <ThumbImage>();

            foreach (var sourceImage in sourceImages)
            {
                ThumbImage thumbnail = sourceImage.GetImage(Format.JPEG, 200, sourceImage.Thumbnails, q: 75);

                SaveThumb(sourceImage, thumbnail, sourceImage.Thumbnails);
                _db.SaveChanges();

                // Add it to colletion of thumbnails
                thumbnails.Add(thumbnail);
            }

            // Set Data to viewmodel
            var vm = new ListSourceImageViewModel
            {
                Page       = page,
                ImageItems = thumbnails
            };

            return(View(vm));
        }
        public EntityEntry <User> AddBlog(string name, string url)
        {
            var blog = _context.User.Add(new User {
                Name = name, SurName = url
            });

            _context.SaveChanges();

            return(blog);
        }
示例#5
0
        public IHttpActionResult PostProduct(Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.Products.Add(product);
            db.SaveChanges();

            return(CreatedAtRoute("DefaultApi", new { id = product.Id }, product));
        }
        public void DeleteStore(string storeName)
        {
            var storeList = (from s in _storeAppContext.Stores
                             where s.Name == storeName
                             select s).FirstOrDefault();

            if (storeList != null)
            {
                _storeAppContext.Stores.Remove(storeList);
                _storeAppContext.SaveChanges();
            }
        }
示例#7
0
        public HttpResponseMessage Block([FromBody] AspNetBlockedUser UserBlock)
        {
            try
            {
                db.AspNetBlockedUsers.Add(UserBlock);
                db.SaveChanges();

                return(CreateMessage(200, "OK"));
            }
            catch (Exception e)
            {
                // Log the error to the database
                LogException(e);
                return(CreateMessage(500, " Internal Server Error"));
            }
        }
示例#8
0
        private void LogException(Exception e)
        {
            try
            {
                db = new BlockedUsersAndMessages();
                ExceptionLog eg = new ExceptionLog(e);

                db.ExceptionLogs.Add(eg);
                db.SaveChanges();
            }
            catch (Exception error)
            {
                // If an exception occured while trying to log the exception it probably means our database is down
                // we can not do any logging from here so we can not handle the exception
            }
        }
示例#9
0
 public void SaveChanges()
 {
     _dbContext.SaveChanges();
 }