コード例 #1
0
        private static void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            if (isRunning)
            {
                return;
            }
            isRunning = true;
            var checkDate = DateTime.Now;

            using (var db = new PeerDb())
            {
                var bindings = db.ArticleToUserBindings.Where(b =>
                                                              b.Deadline <= checkDate && !b.Accepted.HasValue);
                if (bindings.Count() != 0)
                {
                    var uIds = bindings.Select(b => b.UserId).ToList();
                    bindings.Set(b => b.Accepted, false).Update();
                    var users = db.Users.Where(u => uIds.Contains(u.Id)).ToList();
                    foreach (var user in users)
                    {
                        user.Rating -= 1;
                        db.Update(user);
                    }
                }
            }
        }
コード例 #2
0
        public IActionResult Article([FromBody] Article article)
        {
            if (string.IsNullOrEmpty(article.Title))
            {
                return(BadRequest("Title must be set"));
            }
            if (string.IsNullOrEmpty(article.Text))
            {
                return(BadRequest("Text must be set"));
            }
            using (var db = new PeerDb())
            {
                var name = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
                var user = db.Users
                           .FirstOrDefault(u => u.Name == name);
                article.AuthorId   = user.Id;
                article.IsAccepted = null;
                var boArticle = new BO.Article(article);
                db.Insert(boArticle);
                var specialistIds = db.CategoryToUserBindings
                                    .Where(ctub => ctub.CategoryId == article.CategoryId && ctub.UserId != user.Id)
                                    .Select(x => x.UserId).ToList();
                var random         = new Random();
                var reviewersCount = Math.Min(random.Next(1, 5), specialistIds.Count);
                if (reviewersCount != 0)
                {
                    var deadLine         = DateTime.Now.AddDays(1);
                    var addedSpecialists = new List <Guid>();
                    while (addedSpecialists.Count != reviewersCount)
                    {
                        var index        = random.Next(0, specialistIds.Count);
                        var specialistId = specialistIds[index];
                        addedSpecialists.Add(specialistId);
                        specialistIds.Remove(specialistId);
                    }

                    foreach (var sId in addedSpecialists)
                    {
                        db.Insert(new ArticleToUserBinding(sId, boArticle.Id, deadLine));
                    }
                }
                else
                {
                    boArticle.IsAccepted = true;
                    db.Update(boArticle);
                }

                return(Ok($"{boArticle.Id}"));
            }
        }
コード例 #3
0
        public IActionResult ApproveReview([FromRoute] Guid id, int mark)
        {
            if (mark < 1 && mark > 5)
            {
                return(BadRequest("mark can be from 1 to 5"));
            }
            try
            {
                using (var db = new PeerDb())
                {
                    var name = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
                    var user = db.Users
                               .FirstOrDefault(u => u.Name == name);

                    var atb = db.ArticleToUserBindings
                              .Where(a => a.ArticleId == id && a.UserId == user.Id);
                    if (!atb.Any())
                    {
                        return(BadRequest("You have not access"));
                    }
                    atb.Set(a => a.Accepted, true)
                    .Update();
                    db.Insert(new Review(user.Id, mark, id));
                    if (db.ArticleToUserBindings.Where(a => a.ArticleId == id).All(a => a.Accepted == true))
                    {
                        db.Articles.Where(a => a.Id == id).Set(a => a.IsAccepted, true).Update();
                    }
                    user.Rating += 1;
                    db.Update(user);
                }

                return(Ok());
            }
            catch (Exception)
            {
                return(BadRequest("Smth is wrong"));
            }
        }