예제 #1
0
        public JsonResponse GetUploadBuildDetails()
        {
            var fileContent = Request.Files[0];

            if (fileContent == null || fileContent.ContentLength == 0)
            {
                return(new JsonResponse(false, "File was not added into request!"));
            }

            BuildOrderInfo uploadBuild = null;
            BuildOrder     buildEntity = null;

            try
            {
                using (var inputStream = new StreamReader(fileContent.InputStream))
                {
                    var jsonReader = new JsonTextReader(inputStream);
                    var ser        = new JsonSerializer();
                    uploadBuild = ser.Deserialize <BuildOrderInfo>(jsonReader);
                }

                buildEntity = ConvertBuildInfoToDomainModel(uploadBuild);
                var response = _service.GetBuildByName(buildEntity.Name);
                if (response.Result != null)
                {
                    return(new JsonResponse(false, "Build order with name " + buildEntity.Name + " already exists!"));
                }
            }
            catch (Exception ex)
            {
                return(new JsonResponse(false, "File format is not valid!"));
            }

            return(new JsonResponse(buildEntity));
        }
예제 #2
0
        public JsonResponse UploadBuildOrder(string userName, string password,
                                             string name, string versionId, string description,
                                             string race, string vsRace, string buildItems)
        {
            var credResponce = _authService.Login(HttpContext, userName, password, false);

            if (!credResponce.Success)
            {
                return(new JsonResponse(false, "Wrong username or password!"));
            }

            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(versionId) || string.IsNullOrEmpty(race) ||
                string.IsNullOrEmpty(vsRace) || string.IsNullOrEmpty(buildItems))
            {
                return(new JsonResponse(false, "Wrong upload parameters"));
            }

            BuildOrder buildEntity;

            try
            {
                buildEntity = new BuildOrder
                {
                    AddedDate     = DateTime.Now,
                    Race          = race,
                    VsRace        = vsRace,
                    Description   = description,
                    Name          = name,
                    SC2VersionID  = versionId,
                    OwnerUserID   = credResponce.Result.ID,
                    OwnerUserName = userName,
                    BuildItems    = new List <string>()
                };

                var decoded = HttpUtility.UrlDecode(buildItems);
                if (string.IsNullOrEmpty(decoded))
                {
                    return(new JsonResponse(false, "Cant decode string " + buildItems));
                }

                var splitted = decoded.Split(',').ToList();

                var boe = new BuildOrderEncoder(race);
                foreach (var item in splitted)
                {
                    var value = boe.getValue(item);
                    if (!string.IsNullOrEmpty(value))
                    {
                        buildEntity.BuildItems.Add(value);
                    }
                }
            }
            catch (Exception ex)
            {
                return(new JsonResponse(false, "Build data is not in valid format! Exception: " + ex.Message));
            }

            GeneralResponse saveResponse;

            var response = _service.GetBuildByName(buildEntity.Name);

            if (response.Result != null && response.Result.OwnerUserName != userName)
            {
                return(new JsonResponse(false,
                                        "Build order with name " + buildEntity.Name + " already exists and was added by " + userName +
                                        "! Only owner can update existing build order."));
            }

            if (response.Result != null)
            {
                BuildOrder build = response.Result;
                build.BuildItems  = buildEntity.BuildItems;
                build.AddedDate   = DateTime.Now;
                build.Description = description;
                build.Race        = race;
                build.VsRace      = vsRace;

                saveResponse = _service.UpdateBuildOrder(build);
            }
            else
            {
                saveResponse = _service.AddBuildOrder(buildEntity);
            }

            if (saveResponse.Success)
            {
                Log.Activity(HttpContext, string.Format("Build order [{0}] uploaded by {1}", buildEntity.Name, userName));
            }

            return(new JsonResponse(saveResponse));
        }