Esempio n. 1
0
        public async Task <IActionResult> Create(Product product, int?id)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.GetUserAsync(HttpContext.User);

                var store = _context.Stores.Where(i => i.UserId == user.Id).FirstOrDefault();
                product.StoreModelId = store.Id;
                product.Store        = store;
                _context.Add(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            else
            {
            }

            return(View(product));
        }
 public void Add <T>(T entity) where T : class => _Context.Add(entity);
Esempio n. 3
0
        public async Task <IActionResult> BuildAndCreate([Bind("BaseImageTag,VolumeID,UserID,Tag,CreateTime,LastModifiedTime")] Image image)
        {
            var user = await _userManager.GetUserAsync(this.User);

            user.LinuxUser = await _context.LinuxUser.FirstAsync(u => u.ID == user.LinuxUserID);

            image = await validateImage(user, image);

            var clients       = _imageCreationSSEService.GetClients();
            var currentClient = clients.FirstOrDefault(x => _userManager.GetUserId(x.User) == user.Id);
            EventHandler <JSONMessage> handler = async(s, e) =>
            {
                if (currentClient == null)
                {
                    currentClient = clients.FirstOrDefault(x => _userManager.GetUserId(x.User) == user.Id);
                }
                try
                {
                    if (currentClient != null && currentClient.IsConnected)
                    {
                        await currentClient?.SendEventAsync(new ServerSentEvent()
                        {
                            Type = "BuildOutput",
                            Data = new string[] { JsonConvert.SerializeObject(e, new JsonSerializerSettings()
                                {
                                    NullValueHandling    = NullValueHandling.Ignore,
                                    DefaultValueHandling = DefaultValueHandling.Ignore
                                }) }
                        });
                    }
                }
                catch (InvalidOperationException)
                {
                    // Occurs when client offline
                    // TODO: When offline a while, cancel the task
                    currentClient = null;
                }
            };

            image.BuildStatusChanged += handler;
            try
            {
                bool buildResult = await image.CreateAndBuildAsync(_dockerInvoker);

                if (buildResult)
                {
                    currentClient = currentClient ?? clients.FirstOrDefault(x => _userManager.GetUserId(x.User) == user.Id);
                    await currentClient?.SendEventAsync(new ServerSentEvent()
                    {
                        Type = "BuildFinished",
                        Data = new string[] { "ok" }
                    });

                    bool pushResult = await image.PushDockerImageAsync(_dockerInvoker);

                    await currentClient?.SendEventAsync(new ServerSentEvent()
                    {
                        Type = "PushFinished",
                        Data = new string[] { "ok" }
                    });

                    _context.Add(image);
                    await _context.SaveChangesAsync();

                    image.BuildStatusChanged -= handler;
                    await currentClient?.SendEventAsync(new ServerSentEvent()
                    {
                        Type = "Success",
                        Data = new string[] { "ok" }
                    });

                    return(Ok());
                }
                else
                {
                    return(BadRequest());
                }
            }
            catch (System.Exception)
            {
                return(BadRequest());
            }
        }