Exemple #1
0
        public bool UserHasModel(long userId, long moduleId)
        {
            AquiredModules module = context.AquiredModules.Where(d => d.UserId == userId && (d.ModuleId == moduleId || d.Module.InheritedFrom == moduleId)).
                                    FirstOrDefault();

            return(module != null);
        }
        public IActionResult DeleteUsersModule([FromRoute] long userid, [FromRoute] long moduleid)
        {
            IActionResult response = Unauthorized();

            try
            {
                AquiredModules aquiredModule = UserRepository.GetAquiredByUserAndModule(userid, moduleid);
                if (aquiredModule == null)
                {
                    throw new NullReferenceException("User doesn't have this module.");
                }
                UserRepository.DeleteUsersModule(aquiredModule);
                ModuleRepository.Delete(aquiredModule.Module);
                response = Ok(new { result = "The module was deleted successfully." });
            }
            catch (Exception e)
            {
                response = BadRequest(new { e });
            }
            return(response);
        }
        public IActionResult CreateModule([FromBody] ModuleData module)
        {
            IActionResult response = Unauthorized();

            try
            {
                Category category = SubjectRepository.GetCategoryName(module.Category);
                Module   created  = CreateModule(module, category.CategoryId);
                ModuleRepository.Insert(created);

                created.Terms = AddTermsFromIDict(null, module.Terms, created.ModuleId);
                Module         added          = ModuleRepository.Update(created);
                AquiredModules aquiredModules = CreateUserModule(module.Author, created.ModuleId);

                UserRepository.InsertUserModule(aquiredModules);
                response = Ok(new { moduleId = added.ModuleId });
            }
            catch (Exception e)
            {
                response = Ok(new { e });
            }
            return(response);
        }
        public IActionResult GetModule([FromRoute] long userid, [FromRoute] long moduleid)
        {
            IActionResult response = Unauthorized();

            try
            {
                if (IsExist(userid, moduleid))
                {
                    return(Ok(new { result = "User has this module." }));
                }
                Module module     = ModuleRepository.GetById(moduleid);
                int    moduleCost = module.Price;
                User   user       = UserRepository.GetById(userid);
                if (user.Credits >= moduleCost)
                {
                    Module copied = Copy(module);
                    user.Credits -= moduleCost;
                    ModuleRepository.Insert(copied);
                    copied.Terms = CopyTerms(module.Terms, copied.ModuleId);
                    //має працювати
                    Module added = ModuleRepository.Update(copied);

                    AquiredModules aquiredModules = CreateUserModule(userid, added.ModuleId);
                    UserRepository.InsertUserModule(aquiredModules);
                    response = Ok(new { result = "success", moduleId = copied.ModuleId, CreditsLeft = user.Credits });
                }
                else
                {
                    response = Ok(new { result = "Not enough credits." });
                }
            }
            catch (Exception e)
            {
                response = Ok(new { e });
            }
            return(response);
        }
 public AquiredModules InsertUserModule(AquiredModules entity)
 {
     context.AquiredModules.Add(entity);
     context.SaveChanges();
     return(entity);
 }
        public Module GetModuleWithTerms(long userId, long moduleId)
        {
            AquiredModules aquiredModule = context.AquiredModules.Include(r => r.Module).ThenInclude(y => y.Terms).Where(q => q.ModuleId == moduleId).FirstOrDefault(g => g.UserId == userId);

            return(aquiredModule.Module);
        }
Exemple #7
0
 public void DeleteUsersModule(AquiredModules entity)
 {
     context.AquiredModules.Remove(entity);
     context.SaveChanges();
 }