Ejemplo n.º 1
0
        public void TestDeleteActivity()
        {
            PerformanceController ñontroller = ConfigureController();

            DeleteActivityModel m = new DeleteActivityModel {
                id = 1
            };

            IActionResult result = ñontroller.DeleteActivity(m);

            Assert.IsTrue(result is OkObjectResult);

            OkObjectResult okResult = result as OkObjectResult;

            Assert.IsTrue(okResult.Value as string == "Success");

            m = new DeleteActivityModel {
                id = 4
            };

            result = ñontroller.DeleteActivity(m);

            Assert.IsTrue(okResult.Value as string == "Success");
            //Assert.IsTrue(result is BadRequestObjectResult);
        }
Ejemplo n.º 2
0
        public IActionResult DeleteActivity(DeleteActivityModel model)
        {
            if (!ModelState.IsValid)
            {
                var messages = string.Join("; ", ModelState.Values
                                           .SelectMany(x => x.Errors)
                                           .Select(x => x.ErrorMessage));

                return(BadRequest(messages));
            }

            try
            {
                Guid?userId = _ctx.Users.Where(u => u.Login == HttpContext.User.Identity.Name.ToUpper()).SingleOrDefault()?.Id;

                if (null == userId || !userId.HasValue)
                {
                    userId = Guid.Empty;
                    //return BadRequest("User is not authorized");
                }

                _ctx.Database.BeginTransaction();

                if (_ctx.UserActivityDates.Any(ead => ead.UserActivity.UserId == userId && ead.ActivityId == model.id))
                {
                    DateTime today = DateTime.Now.Date;

                    var todayActivities = _ctx.UserActivityDates.Where(ead => ead.UserActivity.UserId == userId &&
                                                                       ead.ActivityId == model.id &&
                                                                       ead.Date >= today);

                    _ctx.UserActivityDates.RemoveRange(todayActivities);
                }

                _ctx.SaveChanges();

                if (!_ctx.UserActivityDates.Any(ead => ead.UserActivity.UserId == userId && ead.ActivityId == model.id))
                {
                    _ctx.UserActivities.RemoveRange(_ctx.UserActivities.Where(ea => ea.UserId == userId && ea.ActivityId == model.id));
                    _ctx.Activities.RemoveRange(_ctx.Activities.Where(a => a.Id == model.id));
                }

                _ctx.SaveChanges();

                _ctx.Database.CommitTransaction();
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }

            return(Ok("Success"));
        }